Practice Makes Perfect,digit_sum


#1



def digit_sum(n):
   n=int(raw_input())
   sum=0
   while (n!=0):
       sum+=n%10
       n=int(n/10)

what is wrong with my code


Replace this line with your code.


#2

there are several problems, here:

n=int(raw_input())

if you want to prompt the user for input, do this outside the function and pass this value as argument into the function call

by overwriting the function parameter (which you currently do), the function no longer works correctly for arguments passed at function call:

def digit_sum(n):
   n=int(raw_input())
   sum=0
   while (n!=0):
       sum+=n%10
       n=int(n/10)

print digit_sum(434)
print digit_sum(253)

see the function calls i added? You prompt the user outside the function and add a function call to pass the user input into the function

the function needs to return the digit sum

for 434 (one of the test cases) the loop never runs. 434 does not equal zero, so the loop never runs. Maybe you need to change your while loop condition?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.