Sobhan Taheri Sobhan Taheri - 2 months ago 5
Python Question

Reversing a number using list in python

I want to reverse a number but I get this error:


"TypeError: 'type' object is not subscriptable"


I would be grateful if you could correct my code.here is my code:

number=input("enter your number ")
num=int(number)
count=0
list1=[]
while(num!=0):
list1.append(num%10)
num=num//10
count=count+1
print(list1[::-1])
k=len(list1)
after=0
for h in range(k):
after+=int(list[h])*(10**h)
h=-1
print(after)

Answer

You can do it easily using list's (create, reverse, join):

''.join(map(str, list(reversed(list(str(num))))))

or just, much easier:

int(str(num)[::-1])
Comments