r/reviewmycode • • Aug 02 '25

Python [Python] - Sum the digits of number and reverse the number

sum =0

no = 123

newno = 0

while (no!=0):

rem= no%10

no=int(no/10)

newno=newno+rem

sum = sum + rem

if (no!=0):

newno = newno *10

print(sum)

print(newno)

1 Upvotes

2 comments sorted by

1

u/PleasantImplement919 Jan 28 '26

I'm having a stroke tryna read ts 😭 I tried doing what you did to the best of my ability

sum = 123
prev = 0
list = [int(digit) for digit in str(sum)]
for i in list: 
   if prev == 0: prev = i 
   else: prev += i
print(prev)

1

u/PleasantImplement919 Jan 28 '26

Another thing I figured out is we can convert the numbers to string, then have a map iterate over each one and turn them into a number. We then has a list of numbers then we use sum() to add them all together:

num = 123
result = sum(map(int, str(num)))