1
class Fibonacci:
    def fn(num):
        if num <= 0:
            return 0
        if num <= 1:
            return 1

        else:
            Result = fn(num - 1) +fn(num - 2)
            return Result

amount = int(input("How many numbers do you want? : "))

i = 1
while i < amount:
    FibValue = fn(i)
    print(FibValue)
    i += 1

Fibonacci()

Here are my errors:

File "C:/Users/Carsten/PycharmProjects/untitled/Lesson13.py", line 30, in <module>
class Fibonacci:
File "C:/Users/Carsten/PycharmProjects/untitled/Lesson13.py", line 45, in Fibonacci
FibValue = fn(i)
File "C:/Users/Carsten/PycharmProjects/untitled/Lesson13.py", line 38, in fn
Result = fn(num - 1) +fn(num - 2)
NameError: name 'fn' is not defined

I'm not quite sure as to why I can run the def Fibonacci: function on its own but as soon as I put it under a class it gives me these errors. I'm still a beginner and have little idea of what these errors mean but even looking them up isn't of much assistance. Any help is appreciated. I realize I can use this as just a standalone function but I'm in the middle of a problem in the youtube series I'm watching to teach myself and I don't want to simply skip ahead in the video and just see the answer to the rest of the problem. Thanks

Prune
  • 76,765
  • 14
  • 60
  • 81
Balla Baby
  • 35
  • 7

1 Answers1

1

When you change the function to a class method (i.e. put it inside the class), then only an object of that class can invoke the function. The syntax is as stanleyli mentioned. Perhaps a little more clear:

fib_obj = Fibonacci()
fib_value = fib_obj.fn(i)

As Ignazio already pointed out, this is a very poor example of Class usage. The class has no attributes (data associated with the object), and making the class only makes it harder to use the function.

A Class is better used when you have a collection of data and functions that act on that data, where the combination embodies a higher-level concept. For instance, a game board with piece locations and movement functions would be a good use of a class.


I understand now. Also see the tutorial references here. Essentially, an iterable is a Class that includes a next (explicit) and/or __next__ (implicit) method. That's why you start with the class structure.

Once you've done that, your main program reduces to

for i in Fibonacci(amount):
    print i
Community
  • 1
  • 1
Prune
  • 76,765
  • 14
  • 60
  • 81
  • thank you very much. I understand. I'm forgetting basics from prior lessons now as well. I'm supposed to create a class that returns values from the Fib sequence each time next is called. I was unsure how to do this and just trying to start out the problem. Still don't fully understand next and why I need a __iter__(self) function as well. I'll cross that bridge in a minute or so though. Thanks for the help – Balla Baby Apr 19 '17 at 00:16
  • Aha! You need to build a *generator* or an *iterator*, not a *class*. – Prune Apr 19 '17 at 00:17
  • Here is the video I am on https://www.youtube.com/watch?v=SFCiaBKwkE4&index=13&list=PLGLfVvz_LVvTn3cK5e6LjhgGiSeVlIRwt and the title is, Learn to Program 13 Iterables : List Comprehensions : Generators. In the directions, he says to create a class that returns values from the Fibonacci sequence each time next is called. Maybe I don't need a class but he wants one for the problem. Anyways. thanks again and I tried upvoting the question but I do not have 15 rep. – Balla Baby Apr 19 '17 at 00:23
  • No problem; I'm just trying to help you with your StackOverflow citizenship. – Prune Apr 19 '17 at 00:26