You can print the Fibonacci sequence n times using a loop in Python. Here's an example code:
main.py200 chars10 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