我想知道是否有人能帮我。我真的是新手,我需要把我的输入转换成一个字符串?你知道吗
# Python program to calculate the Fibonacci numbers
def fibR(n):
if n == 1 or n == 2:
return 1
else:
return fibR(n - 1) + fibR(n - 2)
# Request input from the user
num = int(input("Please enter the number in the Fibonacci sequence you wish to calculate: "))
#
if num == 1:
print("The Fibonacci number you have requested is" + 1 + ".")
else :
print("The Fibonacci number you have requested is" + fibR(num) + ".")
执行^{} 将数字转换为字符串。你知道吗
您已经正确地将
input
转换为int
。但是,在print
语句中。。。你知道吗……函数
fibR(num)
返回一个整数。当您尝试将返回的整数与字符串连接起来时,会导致错误。您需要做的是使用格式字符串:在
else
语句中使用%
:相关问题 更多 >
编程相关推荐