How can I replace an error with a print without closing the program? Python 3

0

If I create a variable with an input that only receives integers (int) and put other types of numbers or even letters, it will generate an error and the program will terminate.

I would like to know if you have a way to replace the error with a custom error message without terminating the program.

    
asked by anonymous 04.01.2018 / 20:42

1 answer

1

You can treat the error with a try / except block, something like this:

def entrar_dados():
    try:
        numero = int(input('Entre com um numero: '))
    except ValueError as e:
        print('Voce nao digitou um numero valido', e)

    # Continuação do código

Adding more rules to your program after it captures value or error, it will continue to run normally.

    
04.01.2018 / 21:38