3

I ran this code for an exercise in python 2.7 but I get the same error everytime, no matter how I call the function fib(n) and I don't know why it doesnt get it. here's the code :

    #!/usr/bin/python

class fibonacci:

    def fib(self,n):
        a=1
        b=0
        c=0
        count=0
        fibo=list()

        while count < n:
            c = a + b
            fibo.append(n)
            fibo.append(c)
            a = b
            b = c
            count += 1
        return fibo

n=int(raw_input("ingrese n: "))
s = fib(n)
print s

when I run it I get this error:

Traceback (most recent call last):
  File "./fib.py", line 22, in <module>
    s=fib(n)
NameError: name 'fib' is not defined
user@debian:~/Documents$ 

help please

2 Answers 2

1

fib() is a method of the class fibonacci, so you have to call it as such:

s = fibonnaci.fib(n)

If you just do fib(n), then the interpreter is looking for a global function named 'fib', outside of any class. In this case, because putting it in a class doesn't provide any specific utility to the function, you could just do this:

def fib(n):
    ...

s = fib(n)

(If you are putting it in a class as a way of namespacing it, keep in mind that Python uses modules to simplify that very thing.)

Sign up to request clarification or add additional context in comments.

2 Comments

i made it as a class because im practising how to work with them.thank you, that worked after some tweaks, but im confused, when i did what you told me i got another error: TypeError: unbound method fib() must be called with fibonacci instance as first argument (got int instance instead) after some fiddling i had to define an init function and feed a fbonacci object to the function, but why?, if the function has a parameter n, shouldn't self be ignored if i input nothing?
Sorry, I shouldn't have glossed over the part about initializing the object. The first argument of any object method is always the object itself (represented by 'self'). That puts the object into the function's scope (in some other languages this happens automatically without showing it as an argument to the method). So when you call fibonacci.fib(n), there are really two arguments being sent: self and n. There is a better explanation of this behavior here: http://stackoverflow.com/questions/2709821/python-self-explained
1
class fibonacci:

    def fib(self,n):
        a=1
        b=0
        c=0
        count=0
        fibo=list()

        while count < n:
            c = a + b
            fibo.append(n)
            fibo.append(c)
            a = b
            b = c
            count += 1
        return fibo

n=int(raw_input("ingrese n: "))
s =fibonacci().fib(n)#<-- make sure to have fibonacci() before you call .fib() otherwise it will throw an error
print s

What you needed was to call the fib function from the class it was in. It was looking at the global scope in which a regular function would be in (one not in a class).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.