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)
6/1/2021 12:28:24 AMJacque Trahan
2 AnswersNew Answer
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)
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!