Home > Software engineering >  Problem with running code and linking parts
Problem with running code and linking parts

Time:11-30

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:")

so when i run the code and input a wrong value first (non integer) then input a number that's not in the menu i want it to be able to let me to then input another number like a right one eg 1 but its seems it doesn't do anything after that and i don't know how to fix it can someone help. It only acknowledges 1-4 only when i do it first but not after any wrong values.

CodePudding user response:

You should use a loop to verify input. Breaking the loop and continuing when the user inputs something valid.

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:"))

Note that this will throw an error if something other than an integer is input. If you want to handle that you should use a try statement.

CodePudding user response:

The reason why this did not happen is because you should wrapp the entire menu, in a while loop. If the entered number is not in the menu, then it'll just display one more time the menu by calling menu(), then it'll ask you to insert one more number selection=input("Select a menu- Input a number:"), and then, it stops running. There is not other instruction to execute next.

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:")
  • Related