0
def GenerateFibonaci(x):

 if(x == 0):
  return 0

 elif(x == 1):
  return 1

 else:
    for i in range(x):
       return GenerateFibonaci(x-1) + GenerateFibonaci(x-2)

 print(GenerateFibonaci(x))

I'm attempting to write a program GenerateFibonaci(x) that takes one input and prints a listed fibonaci sequence depending on the length of x.

Here's what I get as an output, only one value instead of a list:

>>> GenerateFibonaci(6)
8

This is what I'm trying to get it to look like:

>>> GenerateFibonaci(5)
[0,1,1,2,3]
>>> GenerateFibonaci(8)
[0,1,1,2,3,5,8,13]

Not sure if I need to add a list statement or a new nested loop? Any help would be greatly appreciated.

ebroker
  • 27
  • 3
  • may need a `while` loop and a list to append the fibonacci values. – de_classified Feb 19 '20 at 04:30
  • @FishingCode That wouldn't be recursive. – Selcuk Feb 19 '20 at 04:30
  • 2
    A note on style: don't use upper camel case for function names, it makes them look like classes. And don't indent your code with just a single space, and be consistent in the number of spaces indented - 4 is standard and avoid many, many errors that just make you look silly. Apart from that: your example code always returns before printing and returns either a number or a sum of two numbers, so it makes sense you're not seeing a list. Specifically, your `for` loop returns on the first iteration. You could use `yield` and create a generator, if that's what you want. – Grismar Feb 19 '20 at 04:32

0 Answers0