+ 2
Practice 27.2 Summing Digits. Why does my code not work? Please, help
You need to calculate and output the sum all digits of the input number. Sample Input: 643 Sample Output: 13 Explanation: The sum of the digits of 643 is 6+4+3 = 13. Below my solution. The system displays - no output. Code: n = int(input()) sum = 0 while n >= 0: remain = n % 10 n //= 10 sum += remain print(sum)
26 Réponses
+ 2
I have tried it in two ways and they return the same result whatever the length of the input.
https://code.sololearn.com/cwYmXo3T2YHL/?ref=app
+ 2
Thank you very much I copied all your codes to learn. Maybe l will understand the issue when i have more knowledge.
+ 2
I think I know why I extended my version with a list. I did it so I could see the input numbers along with the calculated output and make sure that the output was correct with long ranges of numbers, and as it passed all test cases, I didn't change it to a simpler version.
+ 2
Thanks Quantum very much I have copied your code I woul like to try it too.
+ 2
Quantum thanks so many much, I am so glad finally to find out how to do it.
+ 2
Yaroslav Vernigora Yes, i did a little bit
+ 2
I showed you the online version, where you can see what is happening with your program step by step
+ 2
Calvin Thomas thank you very much, it helhed a lot. It teached me the other possible solutions and that is the most important.
+ 1
Thanks Simba. I changed the >= to the > and this has worked
n = int(input())
sum = 0
while n > 0:
remain = n % 10
n //= 10
sum += remain
print(sum)
+ 1
Did that simple while loop pass all test cases, because when I made it, I had to extend the code to make it work properly with a huge range of digits.🤔
+ 1
I still do not understand the solution.
Test 1. The input - 76789. The output - 37. Below my manual check of the code:
1 iteration.
remain = 76789%10=9
n = 76789//10=7678
sum = 0+9
2 iteration
remain=7678%10=8
n=7678//10=767
sum=9+8=17
3 iteration
remain =767%10=7
n=767//10=76
sum=17+7=24
4 iteration
remain = 76%10=6
n=76//10=7
sum=24+6=30
5 iteration
remain = 7%10=7
n=7//10=0
sum=30+7=37
Uf, hard work.
+ 1
Thank you everyone again. I got it. There was a mistake in my manual above calculation. So now I know.
+ 1
The code is right, check your explanation, after 3th iteration n = 76 (right)
But your n input for the 4th iteration is only 6 , but has to be 76. The putput then will be 6.
„
3 iteration
remain =767%10=7
n=767//10=76
sum=17+7=24
4 iteration
remain = 76%10=6
n=6//10=0
„
+ 1
Thanks Angela a lot. You helped very much.
+ 1
Елена Леващева Just remember to add the output of the list along with the sum of the list, then you can make sure, that the output is correct, especially with long ranges of numbers. Somebody would probably call my code unneccessary, because of the list.
+ 1
Thanks again Quantum and could you kindly explain also how you insert the above my name reference in your post? I failed to find the way.
+ 1
don't thank me...
Visualize your code execution
(Python, Java, C, C++, JavaScript, Ruby)
https://pythontutor.com
+ 1
Yaroslav Vernigora 👍👍✊✊ but why "don't thank me..." ?
+ 1
Yaroslav Vernigora When I work on something, I always create a bunch of outputs from the beginning and to the end of the program. Then I can follow the process during runtime, and it makes it a hole lot easier to localize why the program doesn't behave as expected if the final output is wrong. It works for me, but it probably won't work for everybody, as they would get confused of all those outputs of loops and lists etc.
+ 1
depending on the programming language (for example, in Javascript, the browser itself has a built-in "developer console" tool, where you can do line-by-line debugging of your program) and the tools you work with (for example, IDE). you have the ability to execute the code line by line