Hi, I have a homework assignment to write a function to compute the nth fibonacci number where n is the value input by the user. For example, if n=6, then the result is 8. I saw a problem like this in one of the forums but i would like some feeback on my code, I know ive probably butchered it cuz im new to this. Any help anyone could give me would be great!


def main()
firstnum = 1
secnum = 1
fibnum = getfibnum()

def getfibnum()
c = int(raw_input("Enter fibonacci numer")
if c == 0:
print "fib number is 0"

if c == 1:
print "fib number is 1"

sum = firstnum + secnum
firstnum = secnum
secnum = sum
print sum
main()

I dont think I'm calling the function correctly, of course I'm sure theres alot wrong w/ this code. My teacher doesnt explain things very well, and when I ask for help he just says "You need to read" he doesnt speak very good english so maybe he just doesnt know how to explain things.

When you post code it helps the other forum members out if it is readable. In order to make it readable and not lose the indentation and structure, you must wrap your code in code tags like so:
[code=python] # Code goes in here!

[/code]

On initial inspection I see that your line c = int(raw_input("Enter fibonacci numer") is missing a closing parenthesis. Please repost your code with the code tags and I'll take a nother gander... Also, what exactly is your error?

def main()
   firstnum = 1 = language
   secnum = 1
   fibnum = getfibnum()

def getfibnum()
   c = int(raw_input("Enter fibonaccinumer"))
   if c == 0:
       print "fib number is 0"

   if c == 1:
       print "fib number is 1" 

       sum = firstnum + secnum
       firstnum = secnum
       secnum = sum
       print sum
main()

Alrighty I fixed that parenthesis. The error that im getting right now when i run it is a syntax error on my "if c==0:" statement

You want to take another look at the error message as you would not get to the input statement. The program will stop at "def main()" and "def getfibnum()" Also, take a look at this statement
firstnum = 1 = language
firstnum can either equal 1 or language, but not both.

Finally, the statement "fibnum = getfibnum()" will always result in fibnum = None

Look at post #3 here (and beyond) for the way to program a function http://www.daniweb.com/forums/thread20774.html

Are you actually looking to assign a value to "fibnum"? Because like woooee said, it'll always return None. If you added a line like return sum at the end of the "getfibnum()" function, then "fibnum" would be set to the same value as "sum" within the "getfibnum()" function.
If you only want to call the "getfibnum()" function and not assign a result to a variable, you can just use "getfibnum()" in place of "fibnum = getfibnum()".

*Ahem* You're missing your colon(s)

def getfibnum():
...
def main():
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.