I want to print a fibonacci sequence up to a random number. First I defined a variable (nterm
) and assigned an integer number to it. It is actually the range to which the program will print a fibonacci sequence.
I want to do it by generating a random number, so I included the random library with import random
. Here a random number is generated but I want to assign that number to nterms
(a variable).
How can I assign a random number to nterms
.
import random
print(random.randint(0,5))
# Here a random number should be generated and should be used as nterms
n1 = 0
n2 = 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence upto",nterms,":")
while count < nterms:
print(n1,end=' , ')
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
For example: the random number generated is 5 so it should be equal to nterms.
If nterms = 5
the expected output should be 0 , 1 , 1 , 2 , 3 ,