一个求斐波那契数列的程序,报错说eval的参数不是string,我明明是string啊??
def fbnq(a):
n=eval(a)
if type(n)==type(1):
if n==0:
return 0
elif n==1:
return 1
else:
return fbnq(n-1)+fbnq(n-2)
def main():
a=input('请输入求解第几项:')
print(fbnq(a))
main()
Traceback (most recent call last):
File "E:\a python\No.2\1.py", line 13, in <module>
main()
File "E:\a python\No.2\1.py", line 12, in main
print(fbnq(a))
File "E:\a python\No.2\1.py", line 9, in fbnq
return fbnq(n-1)+fbnq(n-2)
File "E:\a python\No.2\1.py", line 2, in fbnq
n=eval(a)
TypeError: eval() arg 1 must be a string, bytes or code object
然后我把eval()换成int()就正常输出了,为什么??
def fbnq(a):
n=int(a)
if type(n)==type(1):
if n==0:
return 0
elif n==1:
return 1
else:
return fbnq(n-1)+fbnq(n-2)
def main():
a=input('请输入求解第几项:')
print(fbnq(a))
main()