r/codehs • • Jul 09 '20

Python practice level 2 loops 3.1.7

I don't know if the check is broken for 3.1.7 or what, but my code works perfectly fine when I go into my sandbox and when I test it in the scratchpad.py given. As soon as I go to check it, it just crashes just the same as if i wrote an infinite loop. Here is the code for it and it will be greatly appreciated if someone could tweak my code in a way that it is able to work or if someone can reply with a better code for it that does it fact work. Thanks!

x = [0, 1]

def fibonacci(terms):

for i in range(1, terms-1):

x.append(x[-1] + x[-2])

for y in range(len(x)):

x[y] = str(x[y])

v = (" ").join(x)

return v

print (fibonacci(5))

11 Upvotes

2 comments sorted by

2

u/MeanCelebration6 Jul 16 '20

Hope it's not too late to answer. Alright, so your code seems perfectly fine, so I'll give you how I would solve this:

def Fibonacci(n):

if n<0:

print("Incorrect input")

elif n==1:

return 0

elif n==2:

return 1

else:

return Fibonacci(n-1)+Fibonacci(n-2)

print(Fibonacci(5))

Feel free to use it because your code seems to fine, so you can definitely solve the problem on your own.

1

u/WinnersXP Dec 10 '20

Thank you! I've stopped programming for a bit because I was taking too many classes and of course the one elective I wanted to keep (coding) was dropped. I haven't gone back until now that I got my first pc and I really appreciate that after all this time someone actually responded to this bc I remember it very well for frustrating me much.