I am trying to write a simple recursive function in python3. As I am learning OO Java, I also want to write Python code involving objects. Here's my code below. I prompt the user to enter a number and the screen should display every integer smaller until 5.
class Recursion:
@staticmethod
def recursive(x):
if (x>5):
print (x)
recursive(x - 1)
def main(self):
x = int(input('Enter a number for recursive addition: '))
recursive(x)
However, when I run it on a terminal, it says: "NameError: name 'recursive' is not defined". Here's what the error looks like:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Recursion import *
>>> a = Recursion()
>>> a.main()
Enter a number for recursive addition: 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ZycinG/Desktop/Python Practice/Recursion.py", line 9, in main
recursive(x)
NameError: name 'recursive' is not defined
What causes the problem here? I know how to just write the recursive function, give it an argument, and let it run on the terminal. But I want to practice OOP.
mainin the first place? - Tadhg McDonald-Jensenstaticmethodif it doesn't actually use data from an object. - Tadhg McDonald-JensenRecursion().main()below the class definition and entering10when prompted the program works as expected. Well, disregarding that "recursive addition" actually counts down to 6. - Tadhg McDonald-Jensenclass Recursion: @staticmethod def recursive(x): if (x>5): print (x) recursive(x - 1) def main(self): x = int(input('Enter a number for recursive addition: ')) recursive(x)- DropleT