0
Why does this code give no output?
I need to find the sum of the digits in a number. n = int(input()) total = 0 while n > 0: n%10 total = n + n print(total)
13 odpowiedzi
+ 8
James Murphy ,
the program needs to have 2 divudions:
▪︎modulo division '%'
▪︎floor division '//'
n = int(input())
total = 0
while n > 0:
digit = n % 10
total += digit
n = n // 10
print(total)
+ 3
James Murphy, I figured it out.
Sololearn doesn't output when it knows that there is an infinite loop... the print statement is after the infinite loop, thus it'll never print.
0
Maybe do: n = n % 10
0
That doesn't seem to help, still no output.
0
James Murphy, if there is no output, the problem is not in code.. you should have an output because you have a print inside a global scope.
Check the area you are working with
0
Its within a sololearn activity on my phone app so not a lot I can do about the area
0
OK, thanks. How do I make it not be an infinite loop?
0
James Murphy, I don't know what's your need, but I can explain why you get an infinite loop:
- Assume you enter 1 as input.
n = n % 10 -> n = 1
n = n % 10 -> n = 1
n = n % 10 -> n = 1
....
0
OK thanks, that makes sense, I'll have a fiddle with it.
0
Thanks very much, I can see that the floor division is needed to make n no longer be greater than 0, thus breaking the while loop. (I think)
0
i don't understand this
Guess the output of this code:
things = [”text", 0, [1, 2, 8], 4.56]
print(things[2][2])
0
It would be 8 right?
0
n=int(input("Enter your number="))
s=0
a=n
while n>0:
t=n%10
s=s+t
n=n//10
print("\nSum of digit %i is =%i"%(a,s))