我想把它循环回值的类输入不在选项中我正在尝试学习OOP类封装
我已经尝试过在调用中使用“return”和类名或函数,但我没有得到任何运气
class Main:
def main_page(main_1):
print("===== | MAIN MENU |=====")
print("|| ||")
print("========================")
print("1 - STUDENT ")
print("2 - SUBJECT ")
print("3 - QUIT ")
allchoice = ['1', '2', '3'];
choice = input()
while choice not in allchoice:
print("Invalid Please try again")
main_page()
if choice == '1':
print("Hello world")
elif choice == '2':
print("back world")
else:
quit()
mainout = Main()
mainout.main_page()
当我从1,2,3输入数字部分时,我得到这个错误
" Invalid Please try again
Traceback (most recent call last):
File "main.py", line 31, in <module>
mainout.main_page()
File "main.py", line 17, in main_page
main_page()
NameError: name 'main_page' is not defined "
self
。所以你应该写:def main_page(self, main_1):
main_page()
中不使用main_1
,因此可以删除此参数。所以你的函数看起来像:def main_page(self):
我建议你写:
它也解决了你的问题。您试图将
main_page()
称为:main_page()
但另一类方法中的类方法的调用方式如下:
self.main_page()
所以最后的代码是:
您需要将self传递给您的方法,以便在类实例化之后使用它,并在调用self时通过self访问它
self
,你有main_1
,因为你不使用参数input()
而不是print()
choice
总的来说,它看起来像:
样本输出为:
相关问题 更多 >
编程相关推荐