0

How do i sum each number in an input?

One of the challenges in the Python course asks you to output the sum of all digits in a input. I tried really hard to find the right code but everything i do don't seems to work. I can't use sum() to calculate, cause the challenge appears pretty soon in the Python course. I don't want to go further without getting a awnser.

6th Mar 2021, 6:35 PM
OhApyr
OhApyr - avatar
5 Respuestas
+ 3
n = int(input()) sum = 0 while n > 0: sum += n%10 n //= 10 print(sum)
6th Mar 2021, 7:27 PM
visph
visph - avatar
+ 1
Hi! Can we see your code?
6th Mar 2021, 6:44 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
It is a Sololearn Challenge so i cant link the code here but i will copypaste it n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length) this is the base the challenge gives you, you need to change it to make the code read the input and output the sum of the numbers given in the input
6th Mar 2021, 6:57 PM
OhApyr
OhApyr - avatar
- 1
I don't think it's forbidden to use sum. I'd prefer a character based solution instead of arithmetic: print(sum(map(int,input())))
6th Mar 2021, 10:48 PM
Benjamin Jürgens
Benjamin Jürgens - avatar