+ 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
9 Respostas
+ 4
#Shorter
def main():
(num := int(input())) #using walrus python3.8
if(num%9==0):
return 9
else:
return num%9
print(main())
+ 4
داداش از این کد میتونی استفاده کنی:
n=input('enter the number: ')
sum=0
for i in n:
sum+=int(i)
print(sum)
+ 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.
+ 2
total = 0
for x in str(142467):
total += int(x)
print((total - total%10)//10+ total%10)
edited...just noticed your example.
+ 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))
+ 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)
+ 1
ی چیزی یادم رفت بگم دوباره عدد به دست اومده رو ببر تو همین حلقه میتونی تبدیلش کنی به تابع که راحت ینی تابع رو بصورت بازگشتی بنویسی تا جایی که طول رشته عددی به ۱ برسه
0
Very simple
click on the link below you w'll get your answer.
https://code.sololearn.com/c31wiBTWLgeF/?ref=app
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