How to convert int variable to string without deleting zeros?

Asked

Viewed 752 times

0

Need to perform in an activity the validation of characters typed by the user.

My code:

mes1 = int (input ('Digite o mês inicial:'))
valida = len (str (mes1))
while valida != 2:
    print ('Valor inválido! O mês deve ter 2 dígitos.')
    mes1 = int (input ('Digite o mês inicial:'))
    valida = len (str (mes1))

The problem is that when I convert int to string in the'validate 'variable, zeros are disregarded. Ex: Type month 12, all correct; Type month 02, error.

Error image:

erro com os zeros sendo desconsiderados

3 answers

3


An integer value has no zeros left; if you need to keep them you should keep their value as string, but if the intention is to do this to validate a month, don’t. Month 02 is the same as month 2, so validating if it has two characters is not a sufficient validation (42 is a valid month? It has two characters).

If you wish to validate whether a number has been entered and if it is a valid month you will need to treat the exception ValueError which is launched by int when the value is not numerical and to know if it is a valid value for the month just check if it is between 1 and 12 inclusive. Theoretically if I enter "month 2" or "month 02" should be the same thing, then it cannot validate if there are two characters.

while True:
  try:
    month = int(input('Mês: '))
    if 1 <= month <= 12:
      break
    print('Informe um valor entre 1 e 12')
  except ValueError:
    print('Informe um valor numérico')

When executing you would have an exit like:

>>> Mês: a
Informe um valor numérico
>>> Mês: 13
>>> Informe um valor entre 1 e 12
Mês: 5

See working on Repl.it

Important note: One string with zero left is completely different from a value with zero left. In Python 2 the left zero on an integer was used to indicate that the value was octal, that is, base 8 and not base 10. Do, for example, print(010) in Python 2 will display 8, as the number 010 in base 8 equals the number 8 in base 10. In Python 3 this notation has been changed to 0o10, with the letter o between values and started generating a syntax error for integers with 0 left. Be very careful not to confuse things.

2

It’s simple, don’t convert to whole, if you already have the information the way you want it, then use it directly. If you need the conversion, do it after validating. In the end I did the conversion if you still need it, I don’t know if you really need it, it could be a conceptual error there.

I took advantage and removed the redundancy code, now the request appears once, respecting the DRY. improved the organization of the code.

while True:
    mes = input('Digite o mês inicial:')
    if len(mes) == 2:
        break;
    print ('Valor inválido! O mês deve ter 2 dígitos.')
mes1 = int(mes)
print(mes1)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • It is that I wanted at the same time to ensure that it was not possible for the user to type another character, just to type numbers.

-1

Face there is no integer with 0 on the left side 01 == 1 , then reads the input as string and makes string comparison.

  • It is that I wanted at the same time to ensure that it was not possible for the user to type another character, just to type numbers.

Browser other questions tagged

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