如何在循环中使用输入

时间:2012-07-03 20:16:01

标签: python

我创建了一个显示Fibonacci序列中数字的代码。我想要一种方法允许用户输入显示的数字。这是我的代码:

total = 1
total2 = 0
for i in range (*Number of numbers/2*):
    total = total + total2
    print (total)
    total2 = total + total2
    print (total2)
#Shows golden ratio
total3 = total2/total
print (total3)

有人能帮助我吗?太棒了!

2 个答案:

答案 0 :(得分:2)

以下是Python 2.x中有关如何获取整数的示例:

myNum = int(raw_input("Enter a number: "))
print(myNum + 1)

编辑:未经测试的Python 3版本:

myNum = int(input("Enter a number: "))
print(myNum + 1)

答案 1 :(得分:2)

python 2.x:

for i in xrange (int(raw_input())//2):    #use xrange() in python 2.x, 
                                          #it is similar to python 3.x's range()

python 3.x:

for i in range (int(input())//2):