Help with this while loop please-python | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

Help with this while loop please-python

Im having an issue I canā€™t seem to figure out. If i run this code without the while loop I get the first digit I need. Great. Yet Can someone help me understand what I am doing wrong please? I know itā€™s the while loop but it doesnā€™t except n as True no matter no matter why integer input I give n = int(input()) sum = 0 while n > 0: num = n % 10 sum += num print(sum)

1st Jun 2021, 12:28 AM
Jacque Trahan
Jacque Trahan - avatar
2 Respostas
+ 7
The problem is that it is an infinite loop as you never change the value of n. Also, you shouldn't use sum and other built-in names (list, float, min, max) as a variable name as it will redefine them. What you want is; n = int(input()) total = 0 while n > 0: total += n % 10 n //= 10 print(total)
1st Jun 2021, 12:35 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Ah i knew it was something simple. And thanks about the sum usage aspect. I think i actuly saw that in a lesson...or maybe a comment. Thanks again!
1st Jun 2021, 12:50 AM
Jacque Trahan
Jacque Trahan - avatar