【问题标题】:Problem with running code and linking parts运行代码和链接部件的问题
【发布时间】:2022-01-06 13:23:45
【问题描述】:
def menu():
    print("1. Create new User")
    print("2. View User")
    print("3. Update User")
    print("4. Quit ") 
menu()

selection=input("Select a menu- Input a number:")

if not selection.isdigit():
    print("You have input a non digit value. Select again:")
    selection=input("Select a menu- Input a number:")
else:  
    selection = int(selection)
    if selection==1:
      print("::menu 1::")
      newName = input("Input first name : ")
      newSurname = input("Input last name : ")
    elif selection==2:
      print("::menu 2::")
    elif selection==3:
      print("::menu 3::")
    elif selection==4:
      print("you have logged out")
    else:
      print("There is no menu",end=" ")
      print(selection)
      print()
      menu()
      selection=input("Select a menu- Input a number:")

所以当我运行代码并首先输入错误值(非整数)然后输入一个不在菜单中的数字时,我希望它能够让我输入另一个数字,例如正确的数字,例如 1 但它似乎在那之后它没有做任何事情,我不知道如何解决它可以有人帮忙。它只在我第一次做时才承认 1-4,但在任何错误值之后才承认。

【问题讨论】:

    标签: python if-statement input while-loop menu


    【解决方案1】:

    您应该使用循环来验证输入。当用户输入有效的内容时,打破循环并继续。

    selection=int(input("Select a menu- Input a number:"))
    while selection not in (1,2,3,4):
        selection=int(input("Invalid number...Select a menu- Input a number:"))
    

    请注意,如果输入的不是整数,这将引发错误。如果你想处理这个问题,你应该使用try 语句。

    【讨论】:

    • 最好将其包装在一个函数中。
    • @Chris 同意了。肯定有一些改进可以改善可用性/控制流。问题中的整个 else 块也应该是一个函数。我认为最好从一个非常简单的例子开始 OP。
    【解决方案2】:

    没有发生这种情况的原因是您应该将整个菜单包装在一个 while 循环中。如果输入的号码不在菜单中,则调用menu() 会再显示一次菜单,然后它会要求您再插入一个号码selection=input("Select a menu- Input a number:"),然后停止运行。接下来没有其他指令要执行。

    def menu():
        print("1. Create new User")
        print("2. View User")
        print("3. Update User")
        print("4. Quit ") 
    menu()
    selection=input("Select a menu- Input a number:")
    while selection != 4:    
        if not selection.isdigit():
            print("You have input a non digit value. Select again:")
            selection=input("Select a menu- Input a number:")
        else:  
            selection = int(selection)
            if selection==1:
                print("::menu 1::")
                newName = input("Input first name : ")
                newSurname = input("Input last name : ")
            elif selection==2:
                print("::menu 2::")
            elif selection==3:
                print("::menu 3::")
            elif selection==4:
                print("you have logged out")
                break
            else:
                print("There is no menu",end=" ")
                print(selection)
                print()
                menu()
                selection=input("Select a menu- Input a number:")
    

    【讨论】:

      【解决方案3】:

      您可以编写一个小函数来验证您的输入。可能是这样的:

      def validate(selection):
          while not selection.isdigit():
              selection = input("You have input a non digit value. Select again:")
          return int(selection)
      
      def get_name(i):
          print("::menu {}::".format(i))
          newName = input("Input first name : ")
          newSurname = input("Input last name : ")
          return newName, newSurname
          
      def menu():
          print("1. Create new User")
          print("2. View User")
          print("3. Update User")
          print("4. Quit ") 
      menu()
      
      selection = input("Select a menu- Input a number:")
      selection = validate(selection)
      if selection is 1:
          newName, newSurname = get_name(selection)
      elif selection in (2,3):
          print("::menu {}::".format(selection))
      elif selection is 4:
          print("you have logged out")
      else:
          print("There is no menu {}".format(selection))
          menu()
          selection=input("Select a menu- Input a number:")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-13
        • 2020-05-29
        • 2019-05-11
        • 1970-01-01
        • 1970-01-01
        • 2019-02-23
        相关资源
        最近更新 更多