Hello everybody I'm doing a program that finds the nth number in the fibonacci sequence. I actually already coded it. What I would like someone to do is maybe you could help me code it another way. The reason is the last assignment I turned in got a zero, because the code was similar to other students code. I don't talk with other the other students, and I do not share code with the other students.I do not know how me and other students had similar code. I only use online help that may be the reason. Hopefully this second code will not be similar to others and I won't risk the chance of getting another zero.

get the input from the user.

def main():

``````print("This is a program to compute the nth Fibonacci number.")

n = eval(input("What number do you want to know? :"))

a,b,c = 0,1,0
count = 1
``````
create a list first position needs to be one.
The result needs to be 1,1,3,5,8.
not 1,2,3,5,8
``````       Fibonacci = list([1])
while count <n:
c = a + b
a = b
b = c
``````

# add the result into the list.

``````           Fibonacci.append(c)
count = count + 1
print("\nFibonacci number {0}th is: {1}" .format(n, Fibonacci[n-1]))
``````

main()

## All 3 Replies

As a variation use the formula of Binet. This link is in C#, but you'll find your way to it.

Perhaps "using a list" means something more along the lines of the following. Your code is redundant in that the values are in the list and in a, b, & c. Note that a fib sequence can start with any 2 numbers--the following uses zero and one, but you could also use 1 and 1.

``````fib_list=[0, 1]
for ctr in range(10):  ## or while <
fib_list.append(fib_list[-1]+fib_list[-2])

print fib_list
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.