print the fibonacci sequence n times in python

You can print the Fibonacci sequence n times using a loop in Python. Here's an example code:

main.py
n = int(input("Enter the number of terms: "))

# initialize the first two terms
a, b = 0, 1

# loop to generate the sequence up to n terms
for i in range(n):
    print(a, end=' ')
    a, b = b, a + b
200 chars
10 lines

In this code, the variable n stores the number of terms in the sequence that the user inputs. The variables a and b are initialized to 0 and 1, respectively, which are the first two terms of the Fibonacci sequence. Then, the loop goes through n iterations, printing a at each step and updating the variables a and b to generate the next term in the sequence.

The end=' ' argument in the print statement is used to separate the terms of the sequence with spaces instead of newlines.

gistlibby LogSnag