0
What does mystery(9870) return?
def mystery(n): m = " " while n>0: m += str(n %10) n // = 10 return m
3 Answers
+ 3
m += str(n % 10) concatenates the last digit of n to m. n //= 10 divides n with 10.
Iteration 1 : 9870 % 10 = 0 -> m = "0" and n = 9870 // 10 = 987.
Iteration 2 : 987 % 10 = 7 -> m = "0" + "7" = "07" and n = 987 // 10 = 98.
Iteration 3 : 98 % 10 = 8 -> m = "07" + "8" = "078" and n = 98 // 10 = 9.
Iteration 4 : 9 % 10 = 9 -> m = "078" + "9" = "0789" and n = 9 // 10 = 0.
Loop condition fails.
So, the output is "0789".
Note that proper indentation must be done for the code to work.
def mystery(n):
m=""
while(n>0):
m+=str(n%10)
n//=10
return m
0
with n //= 10 and proper indents: " 0789"
0
can you please explain it completely?