I've been asked to write a program that computes the nth Fibonacci number where n is a value input by the user. For example, if n = 6, then the result is 8.
This is what I have so far:
def main():
print "This program will compute nth Fibonacci"
print "number where n is a value input by the user."
x = input("Enter the nth Fibonacci number: ")
s = 1
for i in range(x):
s = s + x
s = s + 1
print "The result is:", s
main()
I believe I'm missing a variable and I can't figure how to get the previous number to add to the next number.
If you want to access or print all the numbers, append them to a list
test_list = [1, 2] ## Fibs can be started with any 2 numbers
test_list.append( test_list[-1]+test_list[-2] )
print test_list
Last edited by woooee; Sep 20th, 2007 at 10:06 pm.
This is very old thread, if you have questions or want critique of your code, start your own fresh thread. Also the OP of this thread wanted to produce single value for nth fibonacci number. That can be made for example like this:
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
Previous Thread in Python Forum Timeline:write strings