Valueerror: invalid literal for int() with base 10: 'eight'

Asked

Viewed 190 times

-5

I need to make treatments if the user type something that is different from numerical values, when I type numerical values the program runs cool, but when I type strings it shows errors.

Traceback (Most recent call last): File "C: Users Student Pycharmproject guppe Data package.py", line 4, in readDinheiro n = int(input(msg)) Valueerror: invalid literal for int() with base 10: 'eight'

def leiaDinheiro(msg):
    while True:
        try:
            n = int(input(msg))
        except (ValueError, TypeError):
            print(f'\033[0;31mERRO: \"{n}\" por favor, digite um número inteiro válido.\33[m')
        else:
            return n

from Pacote import moeda
from Pacote import dados
p = dados.leiaDinheiro('Digite o preço: R$ ')
moeda.resumo(p,20 ,12)
Digite o preço: R$ 200
------------------------------
       RESUMO DO VALOR        
------------------------------
Preço analisado:    R$200,00
Dobro do preço:     R$400,00
Metada do Preço:    R$100,00
20% de aumento:     R$240,00
12%: de redução     R$176,00
------------------------------
  • And what you’ve tried?

1 answer

-2

Well what you are trying to do we call error handling and exceptions, in Python we use try: and except:

recommend to study the official documentation on that subject.

Analyzing your code the treatment is done but in error you send the message containing the number that at that time was not declared, making the changes below you can get the result.

def leiaDinheiro(msg):
while True:
    try:
        n = int(input(msg))
        return n
        break
    except ValueError:
        print(f'\033[0;31mERRO: por favor, digite um número inteiro válido.\33[m')

leiaDinheiro('Digite o preço: R$ '))

Browser other questions tagged

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