我正在尝试在 python3 中编写一个简单的递归函数。在学习 OO Java 时,我也想编写涉及对象的 Python 代码。下面是我的代码。我提示用户输入一个数字,屏幕应该显示每个小于 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)
Run Code Online (Sandbox Code Playgroud)
但是,当我在终端上运行它时,它会显示:“NameError: name 'recursive' is not defined”。这是错误的样子:
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 …Run Code Online (Sandbox Code Playgroud)