Python script- need help understanding this while loop -


my gf studying cs , needs understanding how script runs , why?

what value mystery(9870) return?

def mystery(n):    m = " "     while n > 0:     m += str(n % 10)     n //= 10 return m 

the possible answers are- "789" "0789" "7890" "987" "9870"

we need know how code runs?

can help?

this proper indentation need use.

def mystery(n):       m = ""     while n > 0:         m += str(n % 10)         n //= 10     return m 

when call function:

mystery(9870) ' 0789' 

the function takes parameter , checks if greater 0. while condition satisfied, divides number 10 , converts remainder string , appends empty string m. n //= 10 remove last digit of number , stores remaining in n. , while loop checks if n greater 0 again. etc.. whole thing continues until n single digit number @ point, n//=10 return 0 , condition of while loop not satisfy.

basically, reverses digits of number pass parameter. hope explanation helps.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -