无限而不做它的循环

2024-10-03 00:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把它循环回值的类输入不在选项中我正在尝试学习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                                                                                                                                                                                      "

Tags: inworldmainpagenotfileprintplease
3条回答
  1. 类中的每个方法(static/class除外,它是特殊的)都必须包含第一个参数-self。所以你应该写:

def main_page(self, main_1):

  1. main_page()中不使用main_1,因此可以删除此参数。所以你的函数看起来像:

def main_page(self):

  1. 对用户输入使用递归是一个非常糟糕的主意。而不是:
        while choice not in allchoice:
            print("Invalid Please try again")
            main_page()

我建议你写:

        while choice not in allchoice:
            print("Invalid Please try again")
            choice = input()

它也解决了你的问题。您试图将main_page()称为:

main_page()

但另一类方法中的类方法的调用方式如下:

self.main_page()

所以最后的代码是:

class Main:
    def main_page(self):
        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")
            choice = input()

        if choice == '1':
            print("Hello world")
        elif choice == '2':
            print("back world")
        else:
            quit()

mainout = Main()
mainout.main_page()

您需要将self传递给您的方法,以便在类实例化之后使用它,并在调用self时通过self访问它

class Main:

    def main_page(self):
        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")
            self.main_page()



        if choice == '1':
            print("Hello world")
        elif choice == '2':
            print("back world")


        else:
            quit()

mainout = Main()
mainout.main_page()

  1. 我不认为你打算做一个递归函数
  2. 我想你的意思是self,你有main_1,因为你不使用参数
  3. 循环中需要input()而不是print()
  4. 您需要在循环中重新分配choice

总的来说,它看起来像:

class Main:

    def main_page(self):
        print("===== | MAIN MENU |=====")
        print("||                    ||")
        print("========================")
        print("1 - STUDENT ")
        print("2 - SUBJECT ")
        print("3 - QUIT ")
        allchoice = ['1', '2', '3']
        choice = input(">>>") # clear prompt

        while choice not in allchoice:
            choice = input("Invalid Please try again\n>>>") # another input with clear prompt

        if choice == '1':
            print("Hello world")
        elif choice == '2':
            print("back world")
        else:
            quit()

mainout = Main()
mainout.main_page()

样本输出为:

===== | MAIN MENU |=====
||                    ||
========================
1 - STUDENT 
2 - SUBJECT 
3 - QUIT 
>>>5
Invalid Please try again
>>>6
Invalid Please try again
>>>1
Hello world

Process finished with exit code 0

相关问题 更多 >