+ 3

How do i sum up the numbers within a large number? In python

Example:142467 1+4+2+4+6+7=24=2+4=6

29th Oct 2019, 5:58 PM
MohammadReza Yadollahi
MohammadReza Yadollahi - avatar
9 ответов
+ 4
#Shorter def main(): (num := int(input())) #using walrus python3.8 if(num%9==0): return 9 else: return num%9 print(main())
29th Oct 2019, 8:30 PM
Sousou
Sousou - avatar
+ 4
داداش از این کد میتونی استفاده کنی: n=input('enter the number: ') sum=0 for i in n: sum+=int(i) print(sum)
30th Oct 2019, 6:35 PM
Mohammad Gk
Mohammad Gk - avatar
+ 3
Convert the number in to a string..use a for loop and convert the individual elements back to an int before you do a '+=' to a 'total' variable. If your still stuck, let us know here and i'll post how I've done it...just please have a go yourself.
29th Oct 2019, 6:52 PM
rodwynnejones
rodwynnejones - avatar
+ 2
total = 0 for x in str(142467): total += int(x) print((total - total%10)//10+ total%10) edited...just noticed your example.
29th Oct 2019, 7:29 PM
rodwynnejones
rodwynnejones - avatar
+ 2
sp = lambda x: x if len(str(x))==1 else sp(sum([int(i)for i in str(x)])) print(sp(142467)) #edit: after seeing Sousou solution sp = lambda x:(x%9, 9)[int(x%9==0 and x!=0)] print(sp(142467))
29th Oct 2019, 8:00 PM
Louis
Louis - avatar
+ 1
#in this code you input a numbers in your choice and all the numbers'sum you will get x=input() total=0 for i in x: total=total+int(i) print(total)
30th Oct 2019, 6:40 AM
Souvick Mondal
Souvick Mondal - avatar
+ 1
ی چیزی یادم رفت بگم دوباره عدد به دست اومده رو ببر تو همین حلقه میتونی تبدیلش کنی به تابع که راحت ینی تابع رو بصورت بازگشتی بنویسی تا جایی که طول رشته عددی به ۱ برسه
30th Oct 2019, 6:40 PM
Mohammad Gk
Mohammad Gk - avatar
0
Very simple click on the link below you w'll get your answer. https://code.sololearn.com/c31wiBTWLgeF/?ref=app
31st Oct 2019, 4:00 AM
AJAY LINGAYAT
AJAY LINGAYAT - avatar
0
def sum_up(x): while len(str(x)) != 1: x = sum(list(map(int, list(str(x))))) return x Link to code: https://code.sololearn.com/cKdv3g25xzy9/?ref=app
31st Oct 2019, 9:47 AM
Asman-H
Asman-H - avatar