Creating a Fibonacci Sequence Generator (Beginner Python)

Hi, I am trying to create a Fibonacci sequence generator in Python. This is my code:

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 

When I run this program, I get an error:

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

Thanks for your help, I am trying to teach myself python basic projects.

+4
source share
6 answers

raw_input returns a string. Therefore, we convert d to an integer using:

 d = int(d) 

One more thing: Do not use for d in range(d) . It works, but it's terrible, whatever. Try this method, for example:

 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 

Change I conclude the answer below with additional code tweaking (thanks to the commenters):

 # 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` 
+7
source

Problem

The problem here is that here:

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

you assign a string from input to variable d , and then pass it to range() . But range() expects integers, not strings, and Python doesn't convert them automatically (it leaves the conversion for you).

Decision

The solution is to convert the result of raw_input() to int as follows:

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

and everything will work if you do not provide an integer.

But there is a better (shorter, more efficient, more encapsulated) method of generating Fibonacci numbers (see below).

The best way to generate Fibonacci numbers

I believe this is the best (or almost the best) solution:

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

This is a generator, not a simple function. It is very efficient, its code is short and doesn't print anything, but you can print its result as follows:

 >>> 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 

or convert it to a list:

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

The application of the above in your case

After applying the above code, it may look like this:

 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 

Does your question answer your question?

+4
source

You need to convert the input to a number, for example:

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

In addition, for fun only, the Fibonacci sequence can be expressed more briefly:

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

raw_input returns the type of the string. You need to convert it to int .

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

and range is required int as argument is not string .

That is why when I do

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

So change it to

 for x in range(int(d)): 
+2
source

Simplified Method:

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

1 2 3 5 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 632455861033

+1
source

I see a lot over complex Fibonacci Sequence programs, so I just did it with a while loop; change in number after while loop

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 I know that this is not your program but this is a very simple version; Hope this helps :)

0
source

Source: https://habr.com/ru/post/1392267/


All Articles