S
The problem is that you are confusing a number (a numerical value) with your representation (a string/text representing this number).For example, number 2 is just a "concept", an idea: it represents a certain amount, a numerical value ("two things"). But I can represent this value in several different ways: like the digit 2, or as 2.0, 2,00000, 002, "dois", "two", and in many other ways - these texts are different, but all represent the same number (the same numerical value).So when the user enters 01234567890 and this text is passed to int, the result is the number 1234567890, see:print(int('01234567890')) # 1234567890
That's because by transforming a string to number, zeros to the left don't make a difference in the final value.And by inverting the number 1234567890 using its function, the result is 0987654321 - but that happens because you are concatenating the digits in a string, so the zero appears at the beginning.If you had only used mathematics instead of concatenating strings, the result would be only 987654321. That is, if it were like this:def inverte(n):
sequência = 0
while n > 0:
digito_invertido = n % 10
n //= 10
sequência = sequência * 10 + digito_invertido
return sequência
The result would be 987654321, after all, for numbers, the zero to the left is irrelevant.Then you need to decide what you want to reverse: the exact string that was typed by the user (with zeros to the left and all else) or the numerical value corresponding to what was typed (please remember that the algorithm only works for positive numbers, but we will assume that negative numbers "are not worth").The same goes for the result: should it be a string (possibly with zeros on the left) or the numerical value?If the user type "0012", this is equivalent to number 12, then if inverting the result should be 21 (for I am inverting the numerical value, which is 12) or 2100 (because I reversed the exact string that was typed)?And if you type "1200", the result is 0021 (for it is the string that corresponds to the reverse) or only 21 (it is the numerical value that corresponds to the 0021)?In your case, it seems that you want to consider the zeros to the left of the input (i.e., whether to reverse the string, not the corresponding numeric value) and disregard the output (i.e., take the inverted string and turn into number) - then you would do so:n = input() # se for digitado "01234567890"
print(int(n[::-1])) # 9876543210
That is, first I reverse the string that was typed ( https://stackoverflow.com/q/931092 ). If you type "01234567890", the inverted string will be "09876543210", and then passing that to int the result will be the number 9876543210. You don't even have to do the accounts, because I'm inverting the string and only in the end I see if it's a number.Of course, if you want to check if it really is a number before doing the inversion, you can also:n = input()
try:
int(n) # verifica se foi digitado um número
print(int(n[::-1])) # 9876543210
except ValueError:
print('digite um número válido')