无限而不循环

时间:2019-05-23 15:46:16

标签: python

我希望它循环返回值输入的Class不在我尝试学习OOP Class封装的选项中

我已经尝试在调用中使用带有类名或函数的“ 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                                                                                                                                                                                      "

3 个答案:

答案 0 :(得分:3)

  1. 类中的每个方法(特殊的static / class除外)都必须包含第一个参数-self。因此,您应该写:

def main_page(self, main_1):

  1. 您没有在main_1中使用main_page(),因此可以删除此参数。因此您的函数将如下所示:

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()

答案 1 :(得分:1)

  1. 我不认为您打算使用递归函数
  2. 我认为您的意思是selfmain_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

答案 2 :(得分:0)

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

setEndTimes(pricerules: PriceRule[]) {
    this.endTimes = [];
    pricerules.forEach(pricerule => {
      this.endTimes.push({
        hour: Number(pricerule.endTime.split(':')[0]),
        minute: Number(pricerule.endTime.split(':')[1])
      });
    });
  }

  setStartTimes(pricerules: PriceRule[]) {
    this.startTimes = [];
    pricerules.forEach(pricerule => {
      this.startTimes.push({
        hour: Number(pricerule.startTime.split(':')[0]),
        minute: Number(pricerule.startTime.split(':')[1])
      });
    });
  }