qweeeef 发表于 2021-5-6 20:20:41

兔崽子问题

def fun1(x):
    if x==1|x==2 :
      return 1
    elif x>2:
      return fun1(x-1) + fun1(x-2)
month=int(input("请输入月份:"))
result=fun1(month)
print("第%d月总共有%s只兔子!" % (month, result))

Traceback (most recent call last):
File "D:\新建文件夹\test1.py", line 7, in <module>
    result=fun1(month)
File "D:\新建文件夹\test1.py", line 5, in fun1
    return fun1(x-1) + fun1(x-2)
File "D:\新建文件夹\test1.py", line 5, in fun1
    return fun1(x-1) + fun1(x-2)
File "D:\新建文件夹\test1.py", line 5, in fun1
    return fun1(x-1) + fun1(x-2)

TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

是因为方法返回值没有类型吗?

hrp 发表于 2021-5-6 20:23:42

if x==1|x==2 :
改为
if x==1 or x==2 :
页: [1]
查看完整版本: 兔崽子问题