DEV Community

Matt Ryan
Matt Ryan

Posted on

Fibonacci Sequence

Alt Text

Python script to display the Fibonacci sequence up to n-th term, where n is provided by the user.

# take input from the user
u_input = int(input("\nHow many terms? "))

# first two terms
no_1, no_2 = 0, 1
count = 0

# check if the number of terms is valid
if u_input <= 0:
   print("Please enter a positive integer")
elif u_input == 1:
   print("Fibonacci sequence upto",u_input,":")
   print(no_1)
else:
    # update values
   print("Fibonacci sequence:")
   while count < u_input:
       print(no_1)
       nth = no_1 + no_2
       no_1 = no_2
       no_2 = nth
       count += 1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)