0

I'm doing an assignment and really need help with one aspect of it. I have created a menu function but I need to be able to restrict the user input to just integers but every time I try I'm unable to do so. Is someone able to advise how is best to do this? The code is below :)

def welcomeMessage():

    print("""

Please enter the appropriate number:

1. Connect to server
2. write a file
3. Quit

    """)

while True:
    welcomeMessage()
    selection = int(input("please enter an option from the list"))
    if selection == 1:
        import client
        client.Main()
    elif selection == 2:
        import edit
        edit.Main()
    elif selection == 3:
        input ("press the enter key to exit")
        break
Traff
  • 1
  • Have a look at this answer: https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not and don't convert your input to an integer. – Col Bates - collynomial Jan 05 '21 at 10:51
  • 1
    Instead of checking if it is an integer, I would check if it is one of the numbers you expect. ie `possible_options = [1,2,3]` `selection = None` `while selection not in possible_options:` and then do everything in your while loop. – hostingutilities.com Jan 05 '21 at 10:56

0 Answers0