create a program that reverses the numbers using while, but my program will always ignore 0 at the end, how can I fix it?

Asked

Viewed 60 times

-1

def inverte(n):
    sequência = ''
    while n > 0:
        digito_invertido =  n % 10
        n //= 10
        sequência += str(digito_invertido)
    return sequência

def main():
    n = int(input())
    print(inverte(n))

if __name__ == '__main__':
    main()
  • example : 01234567890
  • expected return: 9876543210
  • outgoing return : 0987654321
  • 1

    Show an example of an entry and the obtained result that you consider is being miscalculated.

  • Unless n = 0, which obviously does not enter while, which other (positive) integer is going wrong?

  • example: 0123456890 should return 09876543210, only it is returning 0987654321, ignoring 0 at the end of the sequence

1 answer

1


The problem is that you are confusing a number (a numerical value) with its representation (a string/text representing this number).

For example, the number 2 is just a "concept", an idea: it represents a certain quantity, a numerical value ("two things"). But I can represent that value in many different ways: as the digit 2, or how 2.0, 2,00000, 002, "dois", "two", and in many other ways - these texts are different, but they all represent the same number (the same numerical value).

Thus, when the user type 01234567890 and this text is passed to int, the result is the number 1234567890, see:

print(int('01234567890')) # 1234567890

This is because, when turning a string to number, the zeros on the left make no difference in the final value.

And by reversing the number 1234567890 using its function, the result is 0987654321 - but this is because you are concatenating the digits into a string, so the zero appears at the beginning.

If you had used only mathematics instead of concatenating strings, the result would be only 987654321. That is, if it were so:

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 on the left is irrelevant.


Then you need to decide what you want to invert: the exact string that was typed by the user (with zeros on the left and everything) or the numeric value corresponding to what was typed (remember that the algorithm only works for positive numbers, but let’s assume that negative numbers "are not worth").

The same goes for the result: must be a string (possibly with zeros on the left) or the numeric value?

If the user type "0012", this is equivalent to the number 12, then if reverse the result should be 21 (because I’m reversing 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 (because it is the string that corresponds to the reverse) or just 21 (is the numerical value corresponding to 0021)?

In your case, it seems that you want to consider the zeros to the left of the input (i.e., you want to invert the string, not the corresponding numeric value) and disregard the output (i.e., take the inverted string and transform it into number) - so you could do so:

n = input() # se for digitado "01234567890"
print(int(n[::-1])) # 9876543210

I mean, first I invert the string that was typed (using the syntax of slicing). If "01234567890" is typed, the string will be reversed "09876543210", and then passing it on to int the result will be the number 9876543210. You don’t even have to do the math because I’m reversing the string and only at the end I see if it’s a number.

Of course, if you want to check if it really is a number before you do 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')

Browser other questions tagged

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