【问题标题】:Python menu-driven programmingPython 菜单驱动编程
【发布时间】:2014-10-17 12:12:49
【问题描述】:

对于菜单驱动的编程,最好的方式是如何编写Quit函数,让Quit只在一个响应中终止程序。

这是我的代码,如果可能,请编辑:

print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
choose=input(">>> ")
choice=choose.lower()
while choice!="q":
    if choice=="v":
        highScore()
        main()
    elif choice=="s":
        setLimit()
        main()
    elif choice=="p":
        game()
        main()
    else:
        print("Invalid choice, please choose again")
        print("\n")
print("Thank you for playing,",name,end="")
print(".")

当程序第一次执行并按“q”时,它会退出。但是在按下另一个功能后,回到 main 并按 q,它会重复 main 功能。 感谢您的帮助。

【问题讨论】:

    标签: python menu python-3.1


    【解决方案1】:

    将菜单和解析放在一个循环中。当用户想退出时,使用break跳出循环。

    来源

    name = 'Studboy'
    while True:
        print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
        choice = raw_input(">>> ").lower().rstrip()
        if choice=="q":
            break
        elif choice=="v":
            highScore()
        elif choice=="s":
            setLimit()
        elif choice=="p":
            game()
        else:
            print("Invalid choice, please choose again\n")
    
    print("Thank you for playing,",name)
    print(".")
    

    【讨论】:

      【解决方案2】:

      在进入循环之前,您只会从用户那里获得一次输入。所以如果他们第一次输入q,那么它就会退出。但是,如果他们不这样做,它将继续遵循输入的大小写,因为它不等于 q,因此不会跳出循环。

      你可以把这段代码分解成一个函数:

      print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
      choose=input(">>> ")
      choice=choose.lower()
      

      然后在进入循环之前调用它,然后作为循环返回之前执行的最后一件事。

      根据 OP 的评论进行编辑:

      下面的代码实现了我提到的分解,就在键入 q 时退出而言,它的工作方式与我预期的一样。

      它已从您的版本中进行了一些调整以在 Python 2.7 中工作(raw_inputinput),并且 nameend 引用已从 @ 中删除987654326@ 所以它会编译(我假设这些是在你的代码中的其他地方定义的)。我还定义了像 game 这样的函数的虚拟版本,以便它可以编译并反映调用行为,这就是这里要检查的内容。

      def getChoice():
          print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
          choose=raw_input(">>> ")
          choice=choose.lower()
      
          return choice
      
      def game():
          print "game"
      
      def highScore():
          print "highScore"
      
      def main():
          print "main"
      
      def setLimit():
          print "setLimit"
      
      
      choice = getChoice()
      
      while choice!="q":
          if choice=="v":
              highScore()
              main()
          elif choice=="s":
              setLimit()
              main()
          elif choice=="p":
              game()
              main()
          else:
              print("Invalid choice, please choose again")
              print("\n")
      
          choice = getChoice()
      
      print("Thank you for playing,")
      

      【讨论】:

        【解决方案3】:
         def Menu:
             while True:
                  print("1. Create Record\n2. View Record\n3. Update Record\n4. Delete Record\n5. Search Record\n6. Exit")
                  MenuChoice=int(input("Enter your choice: "))
                  Menu=[CreateRecord,ViewRecord,UpdateRecord,DeleteRecord,SearchRecord,Exit]
                  Menu[MenuChoice-1]()
        

        【讨论】:

          【解决方案4】:

          这是一个用于矩阵加减的菜单驱动程序

            def getchoice():
                  print('\n What do you want to perform:\n 1.Addition\n 2. Subtraction')
                  print('Choose between option 1,2 and 3')
                  cho = int(input('Enter your choice : '))
                  return cho
          
          
              m = int(input('Enter the Number of row    : '))
              n = int(input('Enter the number of column : '))
              matrix1 = []
              matrix2 = []
          
              print('Enter Value for 1st Matrix : ')
              for i in range(m):
                  a = []
                  for j in range(n):
                      a.append(int(input()))
                  matrix1.append(a)
              print('Enter Value for 2nd Matrix : ')
              for i in range(m):
                  a = []
                  for j in range(n):
                      a.append(int(input()))
                  matrix2.append(a)
              choice = getchoice()
              while choice != 3:
                  matrix3 = []
                  if choice == 1:
                      for i in range(m):
                          a = []
                          for j in range(n):
                              a.append(matrix1[i][j] + matrix2[i][j])
                          matrix3.append(a)
                      for r in matrix3:
                          print(*r)
                  elif choice == 2:
                      for i in range(m):
                          a = []
                          for j in range(n):
                              a.append(matrix1[i][j] - matrix2[i][j])
                          matrix3.append(a)
                      for r in matrix3:
                          print(*r)
                  else:
                      print('Invalid Coice.Please Choose again.')
          
                  choice = getchoice()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-04-23
            • 1970-01-01
            • 2021-11-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多