![](/img/trans.png)
[英]Write a program that asks the user for an hour between 1 and 12 and for how many hours in the future they want to go
[英]How to write a program that asks the user how many Fibonacci numbers to generate and then generates them
这就是我所拥有的,我不知道这是怎么回事。 一直说FibonacciArray [1] = 1行有一个错误,“列表分配索引超出范围”。 请帮忙! 我正在尝试将结果放入数组。 例如,如果用户输入4作为要生成的斐波那契数的数量,我希望它在数组中放入1 1 2 3。
amount = int(input("How many Fibonacci numbers would you like me to create?:"))
FibonacciArray = [amount]
FibonacciArray[0] = 1 #setting first two values of the array to 1
FibonacciArray[1] = 1
FibonacciCounter = 2
if amount == 1:
print (FibonacciArray[0])
elif amount == 2 or amount > 2:
print (FibonacciArray[0])
print (FibonacciArray[1])
else:
while amount > 0: #or while amount != 0
#print out one fibonacci number
FibonacciArray[2] = FibonacciArray[0] + FibonacciArray[1]
print (FibonacciArray[2])
amount = amount - 1 #decrementing amount
FibonacciArray[FibonacciCounter] = FibonacciArray[FibonacciCounter - 2] + FibonacciArray[FibonacciCounter - 1]
print (FibonacciArray[FibonacciCounter])
FibonacciCounter = FibonacciCounter + 1 #increment FibonacciCounter
amount = amount - 1 #decrement amount again
print (FibonacciArray)
您是否不认为放入循环中的条件存在缺陷。 循环将仅达到第三个条件,即,仅当amount
小于1时才达到else
部分。
if amount == 1:
print (FibonacciArray[0])
elif amount == 2 or amount > 2:
print (FibonacciArray[0])
print (FibonacciArray[1])
else:
while amount > 0:
# Remaining code
我假设您是该语言的新手,并且您的许多代码似乎多余或对输出无用。
FibonacciArray = [amount]
FibonacciArray[0] = 1 #setting first two values of the array to 1
FibonacciArray[1] = 1
上面将给您IndexError
因为您可以为现有索引分配值。 您可以将以下三行替换为:
FibonacciArray = [1, 1]
因此,为了向列表中添加新元素,可以使用append()
方法。 以下是代码的pythonic替代方案。
FibonacciArray = []
FibonacciArray.append(1)
FibonacciArray.append(1)
为了访问列表中的最后两个元素,您可以简单地使用negetive index 。
>>> array = [1, 2, 3, 4, 5]
>>> array[-1]
5
>>> array[-2]
4
因此,要找到斐波那契数列中的新元素,您可以这样做,
FibonacciArray.append(FibonacciArray[-1] + FibonacciArray[-2])
print (FibonacciArray)
这是工作代码的链接。 如果需要帮助,请仔细阅读。
FibonacciArray = [amount]
这并不构成长度amount
的数组。 它仅创建一个数组,其中一个条目的值为amount
。 您正在寻找的是这样的:
FibonacciArray = [0]*amount
其次,IIRC, else
情况仅在解析为False
之前的每个if
和elif
语句时发生。 在此声明中:
elif amount == 2 or amount > 2:
您指定该amount
必须大于或等于2。这保证了else
块将永远不会执行。
您的代码还有更多问题,但这些是最大的罪犯。
您的代码应该看起来像这样:
amount = int(input("How many Fibonacci numbers would you like me to create?:"))
if (amount <= 0):
print("Amount cannot be less than 1!")
else:
FibonacciArray = [0]*amount
FibonacciArray[0] = 1
FibonacciCounter = 2
if (amount > 1):
FibonacciArray[1] = 1
if (amount > 2):
while FibonacciCounter < amount:
FibonacciArray[FibonacciCounter] = FibonacciArray[FibonacciCounter - 2] + FibonacciArray[FibonacciCounter - 1]
FibonacciCounter = FibonacciCounter + 1 #increment FibonacciCounter
print(FibonacciArray)
编辑:增加了对负数的处理,我意识到我没有处理案件amount == 1
对。 修复。
创建数组时,可以这样设置:
FibonacciArray = [None] * amount
这将解决您的错误,并将数组值的“数量”数量设置为“无”。 但是您的代码中还有其他错误。 除非输入的数字为0,否则不会调用while循环,因为您的else条件将首先捕获大于2的任何数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.