How to make the system display an error message when it is not a number? [duplicate]

3

How to get the system to return some message of type "This value should be numeric" in the problem below? (menu one and two have already been defined, this is just a cut)

print("MENU PRINCIPAL")
while True:
    esc_menu = int(input("\n1) Ir para o menu um. \n"
                         "2) Ir para o menu dois.\n "
                         "\nEscolha uma opção: "))
    if esc_menu == 1:
        menu_um()
    if esc_menu == 2:
        menu_dois()
    
asked by anonymous 18.11.2017 / 04:28

2 answers

7

The class int raises an exception of type ValueError when the value to be converted to integer is not numeric, so to ensure that the value entered by the user is numeric, just handle the exception.

try:
    esc_menu = int(input("..."))
except ValueError:
    print("O valor deve ser um número inteiro")

As this is a menu, you could do something like:

def menu_um(): print("Menu 1")
def menu_dois(): print("Menu 2")

message = """
1) Ir para o menu um.
2) Ir para o menu dois.

Escolha uma opção: 
"""

menu = {
    1: menu_um,
    2: menu_dois
}

print("Menu")

while True:
    try:
        esc_menu = int(input(message))
        menu[esc_menu]()
    except ValueError:
        print("O valor deve ser um número inteiro")
    except KeyError:
        print("Opção inválida")

See working at Repl.it

Since Python does not have the switch/case structure, you can use a dictionary to store all the functions and thus avoid a large number of if in a row. See that in this case I created the dictionary menu and invoked the proper function doing menu[esc_menu]() . I have also set the string message with the text to be displayed in the menu, because, since there are multiple lines, using the string in triple quotes will facilitate reading and maintaining the code .

    
18.11.2017 / 11:30
3

If it is only integer, you can use the function isdigit() or isnumeric() but you can not convert the entry to int . See:

  

isdigit () : Return true if all characters in the string are alphabetical and there is at least one character, otherwise false. (Free Translation)

     

isnumeric () : Return true if there are only numeric characters in S, False otherwise. Numeric characters include digit characters and all characters that have the Unicode numeric value property. (Free Translation)

print("MENU PRINCIPAL")
while True:
  esc_menu = input("\n1) Ir para o menu um. \n"
  "2) Ir para o menu dois.\n "
  "\nEscolha uma opção: ")
  if esc_menu.isdigit() == False:
    print("Este valor deve ser numérico")

  if esc_menu == "1":
    print("OPCAO 01")
  if esc_menu == "2":
    print("OPCAO 02")
  

Remembering that it worked only for integers.   There may be easier ways.
  isdigit () See working at repl.it
  isnumeric () See working at repl.it

Reference

  

These functions indicate whether a particular string is fully formed by numbers, if the string contains at least one character that invalidates this condition, / p>

    
18.11.2017 / 05:10