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.
5 Respuestas
+ 3
n = int(input())
sum = 0
while n > 0:
sum += n%10
n //= 10
print(sum)
+ 1
Hi! Can we see your code?
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
- 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())))