+ 1

Please help me with this code.

# Write a Python program to read an integer and find its reverse. Note: if user enter 1234 than the output should be 4321. while True: print("Enter a number to get the reverse of it: ") n = [] a = int(input()) n.append(a) b = n.reverse() print(b)

31st Jan 2021, 4:53 PM
Shrish Kumar Saini
Shrish Kumar Saini - avatar
5 Answers
+ 5
Shrish Kumar Saini , there is also an other way to do this task, without using string conversion: reversed_number = 0 n = 128 while n > 0: dig = n % 10 n //= 10 reversed_number = reversed_number * 10 + dig print(reversed_number)
31st Jan 2021, 6:18 PM
Lothar
Lothar - avatar
+ 3
your list will contain the inputed number. reversing a list of one item wil not do something. n = [1234] after reversing it it will stay n = [1234] try to convert the number to string and then reverse that string and convert reverted string back to integer. n = str(a) n.reverse() n = int(n) print(n) or just do this: a = input() a.reverse() a = int(a) print(a)
31st Jan 2021, 5:30 PM
John Robotane
John Robotane - avatar
+ 2
The easiest route is to convert to string, flip it then convert back to integerđŸ€· Check this code https://code.sololearn.com/cTgx4gYfpto8/?ref=app
31st Jan 2021, 5:36 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
thank you everyone :)
11th Mar 2021, 9:04 AM
Shrish Kumar Saini
Shrish Kumar Saini - avatar
+ 1
You can simply do: word = str(input()) word = word[::-1] print(word)
31st Jan 2021, 8:30 PM
Nathanaël Zuchuon
Nathanaël Zuchuon - avatar