## Problem with Fibonacci numbers in Python

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

### Problem with Fibonacci numbers in Python

So I'm practicing my python, and I'm trying to write a program that will list all the Fibonacci numbers, (Because I'm a math nerd, and I love the golden ration) and I have it all figured out, but I want it to stop at 100, (or any other arbitrary number) and I can't figure out how. Before you read my code though, I do not want you to tell me how I could write the same program better yet. I want to finish this one myself and then I would love to hear your thoughts. Also, I still want to figure it out myself, I just need a little help.

so my code is
Code: Select all
`#fibbonacci programfib= [0, 1,]def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    print 0print 1while next_fib < 100:    find_next_fib()print "This is all the fibbonacci numbers up to 100"`

But my problem is next_fib is only defined within the function, find_next_fib().

Again, I don't want the answer, I still want to finish the code myself, but how do I define next_fib universally, instead of within the function? I also tried
Code: Select all
`def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    return next_fib`

and
Code: Select all
`def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    while next_fib < 100:        return true`

but the first one wouldn't define next_fib outside of the function, and the second wouldn't return true outside of the function.

--thty
thehackertoyou
New User

Posts: 18
Joined: Tue Jan 01, 2013 1:27 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

I don't know any python so I can't offer any help with the actual code but maybe you can add an output variable which would make your life a whole lot easier without giving too much away
DrRoach
Poster

Posts: 155
Joined: Fri Feb 22, 2013 6:53 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

I can't find anything on google about output variables (atleast, that makes sense) I also tried making next_fib a global variable but it didn't work.
Code: Select all
`#fibbonacci programfib= [0, 1,]def find_next_fib():    a= len(fib) -1    b= len(fib) -2    global next_fib    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib            print fib[0]print fib[1]while next_fib < 100:    find_next_fib()print "This is all the fibbonacci numbers up to 100"`

but then it just says
while next_fib < 100:
NameError: global name 'next_fib' is not defined

I don't know what to do!

--thty
thehackertoyou
New User

Posts: 18
Joined: Tue Jan 01, 2013 1:27 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

I'm really not much help because I can't see how your code would even run through the fibbonacci sequence in the first place but if I wanted to add a while in C I would put :
Code: Select all
`main();int a = 1;int b = 2;int answer = a + b;printf("%i", answer);a++;b++;while answer < 100;`

Or something along those lines but I'm super tired so am not writing a working script now.
So yeah try and make a variable for next_fib as that's really the only way you can track it. You'll probably end up needing the algorithm to work out the nth term to make this variable though. Sorry if this doesn't help at all.
DrRoach
Poster

Posts: 155
Joined: Fri Feb 22, 2013 6:53 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

I don't know about Python too much either but maybe that doesn't matter.

You don't need a global variable for your function. You can define an array to store fibonacci numbers and pass it as a parameter to a function.

Code: Select all
`fib_numbers = [0, 1]print "%d\n%d" % (fib_numbers[0], fib_numbers[1])for i in range(2, 100):    next_fib(fib_numbers)    print ... def next_fib(array):    last_fib = array[-1]    ...`

pertti123
New User

Posts: 13
Joined: Fri Sep 10, 2010 6:02 am
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

Why aren't you just returning the next fib number? Also, I'm going to ignore your request for critiquing and say,

USE A MAIN FUNCTION

Also, don't use globals, ever.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.

fashizzlepop
Developer

Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

fashizzlepop wrote:Also, don't use globals, ever.

I know this is bad practice, but sometimes it makes me wonder. Why would they have added such things, when all people will tell you to do is not to use it?

For those that know
K: 0x2CD8D4F9

-Ninjex-
Moderator

Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

fashizzlepop wrote:Why aren't you just returning the next fib number? Also, I'm going to ignore your request for critiquing and say,

USE A MAIN FUNCTION

Also, don't use globals, ever.

I did return next_fib
Code: Select all
`def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    return next_fib`

although I just realized that next_fib is still undefined because I hadn't ever ran the function, find_next_fib yet.
So I tried this,
Code: Select all
`#fibbonacci programfib= [0, 1,]def find_next_fib():    a= len(fib) -1    b= len(fib) -2    global next_fib    next_fib = fib[a] + fib[b]    fib.append(next_fib)    return next_fib    print next_fib            print fib[0]print fib[1]find_next_fib()while next_fib < 100:    find_next_fib()print "This is all the fibbonacci numbers up to 100"`

But then I got this (without any errors.)

0
1
This is all the fibbonacci numbers up to 100

Also, how is a main function different than a normal function?
And why not use globals?
thehackertoyou
New User

Posts: 18
Joined: Tue Jan 01, 2013 1:27 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

When a function returns it stops executing (that's why you aren't printing anything from the function). Also, you aren't capturing the return values, ever.

A main function is the start of your program and keeps everything more organized. See this stack overflow question.

As for globals, the reason they are bad is fairly simple. They don't belong to anything, they live everywhere, and they have no scope. This makes for messy code. Read http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them and specifically read the comments to the first answer.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.

fashizzlepop
Developer

Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)

### Re: Problem with Fibonacci numbers in Python

thehackertoyou wrote:So I'm practicing my python, and I'm trying to write a program that will list all the Fibonacci numbers, (Because I'm a math nerd, and I love the golden ration) and I have it all figured out, but I want it to stop at 100, (or any other arbitrary number) and I can't figure out how. Before you read my code though, I do not want you to tell me how I could write the same program better yet. I want to finish this one myself and then I would love to hear your thoughts. Also, I still want to figure it out myself, I just need a little help.

so my code is
Code: Select all
`#fibbonacci programfib= [0, 1,]def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    print 0print 1while next_fib < 100:    find_next_fib()print "This is all the fibbonacci numbers up to 100"`

But my problem is next_fib is only defined within the function, find_next_fib().

Again, I don't want the answer, I still want to finish the code myself, but how do I define next_fib universally, instead of within the function? I also tried
Code: Select all
`def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    return next_fib`

and
Code: Select all
`def find_next_fib():    a= len(fib) -1    b= len(fib) -2    next_fib = fib[a] + fib[b]    fib.append(next_fib)    print next_fib    while next_fib < 100:        return true`

but the first one wouldn't define next_fib outside of the function, and the second wouldn't return true outside of the function.

--thty

Think of this. if you use a function in a loop like for x in range(number) it will repeat that function "number" times.
http://lawofcode.com
What will you learn?
Kataclysmic
New User

Posts: 27
Joined: Wed Oct 09, 2013 10:15 pm
Blog: View Blog (0)

Next