Skip to content

Instantly share code, notes, and snippets.

@phanthaihuan
Created June 13, 2019 08:01
Show Gist options
  • Save phanthaihuan/618900b9a5de3d2ba063b93839a0567c to your computer and use it in GitHub Desktop.
Save phanthaihuan/618900b9a5de3d2ba063b93839a0567c to your computer and use it in GitHub Desktop.
fibonnaci
#Exercise 13 (and Solution)
#Write a program that asks the user how many Fibonnaci numbers to generate and then generates them. Take this opportunity to think about how you can use functions. Make sure to ask the user to enter the number of numbers in the sequence to generate.(Hint: The Fibonnaci seqence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)
def fib(n):
a = []
if n == 1:
a.append(1)
return a
else:
a = [1, 1]
i = 2
while i < n:
x = a[i-1] + a[i-2]
a.append(x)
i = i+1
return a
while True:
n = input('Enter a number or "q" to quit: ')
if n == "q":
break
else:
print("Fibonnaci: " + str(fib(int(n))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment