创建fibonacci序列生成器(初学者Python)

时间:2012-01-21 22:35:25

标签: python

您好我正在尝试用Python创建一个Fibonacci序列生成器。这是我的代码:

d =raw_input("How many numbers would you like to display")

a = 1
b = 1

print a
print b

for d in range(d):
    c = a + b 
    print c
    a = b
    b = c

当我运行此程序时,我收到错误:

File "Fibonacci Sequence Gen.py", line 10, in <module>
    for d in range(d):
TypeError: range() integer end argument expected, got str

感谢您的帮助,我正在尝试用基本项目教自己python。

6 个答案:

答案 0 :(得分:7)

raw_input返回一个字符串。所以将d转换为整数:

d = int(d)

还有一件事:不要使用for d in range(d)。它有效,但它是可怕的,无声的,无论如何 试试这种方式,例如:

numbers = raw_input("How many numbers would you like to display")

a = 1
b = 1

print a
print b

for d in range(int(numbers)):
    c = a + b 
    print c
    a = b
    b = c

修改:我在答案的下方完成了额外的代码调整(感谢评论者):

# one space will separate better visually question and entry in console
numbers = raw_input("How many numbers would you like to display > ")    

# I personnally prefer this here, although you could put it
# as above as `range(int(numbers))` or in `int(raw_input())`
# In a robust program you should use try/except to catch wrong entries
# Note the number you enter should be > 2: you print 0,1 by default
numbers = int(numbers)  

a, b = 0, 1        # tuple assignation
                   # note fibonnaci is 0,1,1,2,3...

print a            # you can write this as print "%i\n%i" % (a, b)
print b            # but I think several prints look better in this particular case.

for d in range(numbers - 2):  # you already printed 2 numbers, now print 2 less
    c = a + b 
    print c
    a, b = b, c    # value swapping.
                   # A sorter alternative for this three lines would be:
                   # `a, b = b, a + b`
                   # `print b` 

答案 1 :(得分:4)

问题

这里的问题是:

d = raw_input("How many numbers would you like to display")

您将输入中的字符串分配到d变量,然后将其传递给range()。但是range()期望整数而不是字符串,并且Python不会自动转换它(它会转换给你)。

解决方案

解决方案是将raw_input()的结果转换为int,如下所示:

d = int(raw_input("How many numbers would you like to display"))

除非你提供非整数,否则一切都会有效。

但是有更好的(更短,更有效,更封装)的方法来生成斐波纳契数(见下文)。

生成斐波纳契数的更好方法

我相信这是最好的(或几乎是最好的)解决方案:

def fibo(n):
    a, b = 0, 1
    for i in xrange(n):
        yield a
        a, b = b, a + b

这是一个生成器,而不是一个简单的函数。它效率很高,代码很短,不会打印任何东西,但你可以打印它的结果:

>>> for i in fibo(20):
    print i,


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

或将其转换为类似的列表:

>>> list(fibo(20))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

在您的案例中应用上述内容

将以上内容应用到您的代码后,它可能如下所示:

def fibo(n):
    a, b = 0, 1
    for i in xrange(n):
        yield a
        a, b = b, a + b

d = int(raw_input("How many numbers would you like to display"))
for i in fibo(d):
    print i

它能回答你的问题吗?

答案 2 :(得分:3)

您必须将输入转换为数字,如下所示:

d = int(raw_input("How many numbers would you like to display: "))

另外,只是为了好玩,斐波那契序列可以更简洁地表达:

a, b = 0, 1
for i in range(d):
    print a
    a, b = b, a+b

答案 3 :(得分:2)

raw_input返回字符串类型。您需要将其转换为int

>>> x = raw_input()
2
>>> x
'2'
>>> type(x)
<type 'str'>

range函数需要int作为参数而不是string

这就是我做的时候

>>> range(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got str.

所以,将其改为

for x in range(int(d)):

答案 4 :(得分:1)

更简单的方法:

a = [0,1]
for n in range(1,41):
  a.append(a[n]+a[n-1])
  print a[-1]

1 2 3 五 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141

答案 5 :(得分:0)

我看到很多复杂的Fibonacci Sequence程序,所以我只是用while循环做了这个;在while循环后更改数字

a = 0 b = 1 while a <= 1000000000: #Changing this number will change how long the sequence goes on for print(a) print(b) a = a+b b = b+a 我知道这不是你的程序,但它是一个非常基本的版本; 我希望这会有所帮助:)