number thereafter is the sum of the two preceding numbers. Write a program that asks the
user how many Fibonacci numbers to print and then prints that many.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . .
num = int(input("Enter a number: "))
a, b = 1, 1
for i in range(num):
print(a)
a, b = b, a + b
This code will use the input()
function to get a number from the user, and then it will use a for
loop to generate the specified number of Fibonacci numbers. The a
and b
variables are initialized to 1, and then they are updated on each iteration of the loop to be the sum of the previous two numbers.
To run this code, you can save it to a file with a .py
extension and run it using the python
command in a terminal or command prompt.
0 Comments