problem with Try and except in a simple python program

Asked

Viewed 71 times

-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

1 answer

-3


To get the desired result we can use a specific error handling for each input as follows:

    def questao3():
    try:
        nome_produto = input('Digite o nome do produto: ')

        while True:
            preco = float(input('Digite o preco desse produto: '))
            if preco <= 0 or preco > 100:
                print('valor invalido, o preco deve ser um valor maior q 0 e menor que 100.')
            else:
                break

        while True:
            quantidade = int(input('Digite a quantidade do produto: '))
            if quantidade < 0 or quantidade > 1000:
                print('a quantidade deve ser um inteiro entre 0 e 1000')
            else:
                break
    except KeyboardInterrupt:
        print('Encerrando o programa...')
        return -1
    else:
        lista = [nome_produto, preco, quantidade]
        return lista

print(questao3())

This way the user will be stuck in each entry until you enter a valid value and, if wrong will receive the error message set.

  • You are an angel!! Thank you, you helped me so much

  • It was nothing! In case you need anything else you can count on me

  • soon I will need even kkk again thanks

  • 2

    @matheussantos Just one detail: make an exception (with raise) within a block try, just to force you to fall into except, is a crooked use of exceptions. In this case, the simplest is to put the error message in the if and ready. Especially because they are different situations: if you fall into the except is because a number has not been typed, and if it falls on the if is because a number has been typed but it is not in the correct range. Another detail is that the continue there in the first while is redundant and can be removed. Suggestion: https://ideone.com/mFzbAQ - or: https://ideone.com/6BWhX4

  • Thanks for the addendum, I’ll improve the answer.

  • I wanted to know why you had such a negative vote on that question. I did something wrong ????

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.