python 中的斐波那契排序困难
fibonacci sequencing difficulties in python
(我学的是基础计算机科学class,这是作业)
我正在尝试使用 "n" 作为参数创建一个基本的斐波那契数列。
当我 运行 程序处于空闲状态
时,我到目前为止似乎工作正常
def fibonacci(n):
a=0
b=1
n = input("How high do you want to go? If you want to go forever, put 4ever.")
print(1)
while stopNumber=="4ever" or int(stopNumber) > a+b:
a, b = b, a+b
print(b)
fibonacci(n)
但是当我尝试 运行 程序以便它显示信息时,我得到了这个错误
Traceback (most recent call last):
File "C:/Users/Joseph/Desktop/hope.py", line 10, in <module> fibonacci(n)
NameError: name 'n' is not defined
知道如何解决这个问题吗?
由于您的 fibonacci
函数正在接受输入,因此不需要传递参数。但是如果你的错误 n
没有在全局范围内定义。我只想去掉 n
参数。另外,只需将 stopNumber
替换为 n
.
def fibonacci():
a=0
b=1
n = input("How high do you want to go? If you want to go forever, put 4ever.")
print(1)
while n == "4ever" or int(n) > a+b:
a, b = b, a+b
print(b)
fibonacci()
您不应在调用 fibonacci
时传递尚未从用户那里读取的 n
。此外,您使用的是 stopNumber
(而不是 n
)。我想你想要
def fibonacci():
a=0
b=1
stopNumber = input("How high do you want to go? " +
"If you want to go forever, put 4ever.")
print(1)
while stopNumber=="4ever" or int(stopNumber) > a+b:
a, b = b, a+b
print(b)
fibonacci()
(我学的是基础计算机科学class,这是作业)
我正在尝试使用 "n" 作为参数创建一个基本的斐波那契数列。
当我 运行 程序处于空闲状态
时,我到目前为止似乎工作正常def fibonacci(n):
a=0
b=1
n = input("How high do you want to go? If you want to go forever, put 4ever.")
print(1)
while stopNumber=="4ever" or int(stopNumber) > a+b:
a, b = b, a+b
print(b)
fibonacci(n)
但是当我尝试 运行 程序以便它显示信息时,我得到了这个错误
Traceback (most recent call last):
File "C:/Users/Joseph/Desktop/hope.py", line 10, in <module> fibonacci(n)
NameError: name 'n' is not defined
知道如何解决这个问题吗?
由于您的 fibonacci
函数正在接受输入,因此不需要传递参数。但是如果你的错误 n
没有在全局范围内定义。我只想去掉 n
参数。另外,只需将 stopNumber
替换为 n
.
def fibonacci():
a=0
b=1
n = input("How high do you want to go? If you want to go forever, put 4ever.")
print(1)
while n == "4ever" or int(n) > a+b:
a, b = b, a+b
print(b)
fibonacci()
您不应在调用 fibonacci
时传递尚未从用户那里读取的 n
。此外,您使用的是 stopNumber
(而不是 n
)。我想你想要
def fibonacci():
a=0
b=1
stopNumber = input("How high do you want to go? " +
"If you want to go forever, put 4ever.")
print(1)
while stopNumber=="4ever" or int(stopNumber) > a+b:
a, b = b, a+b
print(b)
fibonacci()