0

Listen, I know there are a thousand posts about this, and I've spent three hours looking at them all. I know there's a simple reason this code is not working, but I can't figure it out. When it runs, I only want it to return the nth term. I am new to python, and maybe there's some logic I'm not getting. So when I do this:

n = int(input("What Fibonacci number would you like to find? "))

def fib(n):
    i = 0
    present = 1
    previous = 0

    while i < n:
        nextterm = present + previous
        present = previous
        previous = nextterm
        i = i + 1
        print nextterm

print(fib(n))

I get this:

1
1
2
3
5
None

When I do this:

n = int(input("What Fibonacci number would you like to find? "))

def fib(n):
    i = 0
    present = 1
    previous = 0

    while i < n:
        nextterm = present + previous
        present = previous
        previous = nextterm
        i = i + 1
        return nextterm

print(fib(n))

I just get "1"

I have spent so much time on this and I am so confused. Someone please fix me!

brinraeven
  • 33
  • 1
  • 4
  • 1
    In the first one you never return a value. In the second one you return immediately. – abc Sep 02 '18 at 01:15

4 Answers4

2

You just forgot to return the value:

n = int(input("What Fibonacci number would you like to find? "))

def fib(n):
    i = 0
    nextterm = 1
    present = 1
    previous = 0

    while i < n:
        nextterm = present + previous
        present = previous
        previous = nextterm
        i = i + 1
        #print nextterm

    return nextterm

print(fib(n))
Schalton
  • 2,867
  • 2
  • 32
  • 44
0

In the first case you are not returning anything, so when you print(fib(n)), since python doesn't find any return statement, it returns None, that's the last print you see, the others are the ones inside the loop. In the second case you have the return statement inside the while loop, so when it performs the first iteration it returns 1, goes out of the frame and no other iteration is executed, so it just prints '1'.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
gcapittini
  • 11
  • 1
  • 1
  • @OzzyWalsh Not necessarily the answer must contain code, many times it is only necessary to indicate what is missing, please read [answer] :) – eyllanesc Sep 02 '18 at 01:38
0

Because return only returns once, the first time return is done,

Clearer example:

 def f():
     return 'good'
     return 'bad'

Now:

print(f())

Returns:

'good'

in the other hand this is a way to solve it too:

n = int(input("What Fibonacci number would you like to find? "))

def fib(n):
    l=[]
    i = 0
    present = 1
    previous = 0

    while i < n:
        nextterm = present + previous
        present = previous
        previous = nextterm
        i = i + 1
        l.append(nextterm)
    return '\n'.join(str(i) for i in l)
print(fib(n))

Or print with out doing another print:

n = int(input("What Fibonacci number would you like to find? "))

def fib(n):
    i = 0
    present = 1
    previous = 0

    while i < n:
        nextterm = present + previous
        present = previous
        previous = nextterm
        i = i + 1
        print(nextterm)
fib(n)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • I'm not completely sure that either option is what OP actually wants, but you certainly help understand how to get what they do want, so nice work. – Mad Physicist Sep 02 '18 at 01:59
-1
def fibonacci():
    num = int(input("How many numbers that generates?:"))
    i = 1
    if num == 0:
        fib = []
    elif num == 1:
        fib = [1]
    elif num == 2:
        fib = [1,1]
    elif num > 2:
        fib = [1,1]
        while i < (num - 1):
            fib.append(fib[i] + fib[i-1])
            i += 1
    return fib
print(fibonacci())
tez
  • 7
  • 3
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Sep 02 '18 at 02:17