-4
def questao3():
while True:
try:
nome_produto = input('Digite o nome do produto: ')
preco = float(input('Digite o preco desse produto: '))
while preco <=0 or preco > 100:
raise ValueError
quantidade = int(input('Digite a quantidade do produto: '))
while quantidade < 0 or quantidade > 1000:
raise ValueError
except KeyboardInterrupt:
print('Encerrando o programa...')
return -1
except ValueError:
print('valor invalido, o preco deve ser um valor maior q 0 e menor que 100.')
print('a quantidade deve ser um inteiro entre 0 e 1000')
else:
lista = [nome_produto, preco, quantidade]
return lista
print(questao3())
I would like to know how to print a different message for each case, if the answer cannot be converted to float (in price) and cannot be converted to int(in quantity). Both fit in Valueerror, but not knowing how to print a message for each. I would like to know how to make the program insist on the same question until the answer is correct (instead of restarting the program) I thank you already
You are an angel!! Thank you, you helped me so much
– matheus santos
It was nothing! In case you need anything else you can count on me
– Gabriel Gomes Nicolim
soon I will need even kkk again thanks
– matheus santos
@matheussantos Just one detail: make an exception (with
raise
) within a blocktry
, just to force you to fall intoexcept
, is a crooked use of exceptions. In this case, the simplest is to put the error message in theif
and ready. Especially because they are different situations: if you fall into theexcept
is because a number has not been typed, and if it falls on theif
is because a number has been typed but it is not in the correct range. Another detail is that thecontinue
there in the firstwhile
is redundant and can be removed. Suggestion: https://ideone.com/mFzbAQ - or: https://ideone.com/6BWhX4– hkotsubo
Thanks for the addendum, I’ll improve the answer.
– Gabriel Gomes Nicolim
I wanted to know why you had such a negative vote on that question. I did something wrong ????
– matheus santos