0
I need to learn how to check if the input will result in a Valueerror, and then re-run the input request until the value is an integer.
My problem:
QuantidadeCabos = int(input("Digite a quantidade de cabos: "))
If the user type a string for example then soon 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 number!"
and then immediately re-run the input request.
Edit:
It worked, but when I call the variable it says it hasn’t been defined,?
Nameerror: name 'Quantidadecabos' is not defined
I did it this way:
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 ran a test to see if the value is correct need to be inserted between 3 and 6:
(Here I haven’t 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: "))