Write a python program to do the following: Ask the user for a number X Store X
ID: 3889606 • Letter: W
Question
Write a python program to do the following:
Ask the user for a number X
Store X numbers of the fibonacci sequence in a list
If the user inputs a number less than 3 tell them it must be 3 or greater
At the end, print out the whole list with their index values (example at bottom)
The fibonacci sequence is produced starting with 0,1
Every following number is the summation of the previous 2. So the 3rd number would be 0+1
0,1,1
the 4th number would be 1+1 and so on
0,1,1,2
If the user enters in 8 the sequence should be
0,1,1,2,3,5,8,13,21
For printing with index values the program would print the following( format is index:value)
0 : 0
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
submit the python file firstname_lastname_fib.py
Explanation / Answer
#This is python program to print the Fibonacci series upto the X-th term where X is provided by the user and minimum value of X provided by the user should be 3
#Get the input from user
x = int(input("Enter the number X"))
if(x<3):
#As number is not greater than or equal to 3 we can not proceed
print("Please enter a number greater than or equal to 3")
else:
#Create a list to store the fibonacci series and store the initial two (0,1) elements
fibonacciList = [0,1]
#starting count from 2 as initial two elements(0,1) are already in list
count = 2
#iterating to create next elements of fibonacci series till X-th term
while(count<=x):
#adding previous two elements to get the next element in fibonacci series like- 3rd number will be 0+1 = 1 and 4th number will be 1+1 = 2
fibonacciList.append(fibonacciList[count-1] + fibonacciList[count-2])
count += 1
#Iterating over the created fibonacci list to print the values in format of index : value
for index, value in enumerate(fibonacciList):
print (index,":",value)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.