I need to learn how to check if the input will result in a ValueError, and then re-execute the input request until the value is an integer.
My problem:
QuantidadeCabos = int(input("Digite a quantidade de cabos: "))
If the user types a string for example soon after I already get this error message
ValueError: invalid literal for int() with base 10: 'teste'
I wanted to prevent this error and warn the user for example "Enter an integer!"
and then re-execute the input request.
Edit:
It worked, but when I call the variable says it has not been defined, sorry for the stupidity how do I solve this problem?
NameError: name 'QuantityCables' is not defined
I did as follows:
def perguntacabos():
QuantidadeCabos = input("Digite a quantidade de cabos: ")
try:
return int(QuantidadeCabos)
except ValueError as err:
print("Digite um Número inteiro!")
return perguntacabos()
I called the function:
perguntacabos()
And then I made a test to see if the value is correct I need to be inserted between 3 and 6:
(Here I have not changed anything yet, what do I do to work?)
while QuantidadeCabos < 3 or QuantidadeCabos > 6:
print("Digite um valor entre 3 e 6!")
QuantidadeCabos = int(input("Digite a quantidade de cabos: "))