I have a problem about making a fibonacci sequence to a list, I'm just new to python someone help me please.
This is my code. I know this is looking wrong or something because it says invalid syntax. I don't know what to do about this really :(
This code works for a normal code without using a list!
myArray1 = [0]
myArray2 = [1]
while myArray2 < 700:
myArray1, myArray2 = b[i], myArray1+myArray2[i]
print(myArray2)
This code puts the first 700 fibonacci numbers in a list. Using meaningful variable names helps improve readability!
fibonacci_numbers = [0, 1]
for i in range(2,700):
fibonacci_numbers.append(fibonacci_numbers[i-1]+fibonacci_numbers[i-2])
Note: If you're using Python < 3, use xrange instead of range.
You may want this:
In [77]: a = 0
...: b = 1
...: while b < 700:
...: a, b = b, a+b
...: print a, b
1 1
1 2
2 3
3 5
5 8
8 13
13 21
21 34
34 55
55 89
89 144
144 233
233 377
377 610
610 987
If you wanna store the results in a list, use list.append:
In [81]: a = 0
...: b = 1
...: fibo=[a, b]
...: while b < 70:
...: a, b = b, a+b
...: fibo.append(b)
...: print fibo
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With