Deleted


#1




#3

you can't use sum as a variable, because it has another function

def digit_sum(n):
    n = str(n)
    summ = 0
    for i in n:
        i = int(i)
        summ += i
    return summ

also everytime you are defining the summ variable in your code, your making it 0 again, which is why in the end it becomes 4


#4

you can try it.

def digit_sum(n):
    sum = 0
    for i in str(n):
        sum = sum + int(i)
    return sum

#5

def digit_sum(n):
num = int(raw_input("Please enter a two digit number: ")
second = num%10 #if no. is 21, modulo gives 1
return second
first = num/10
return first
summ = 0
summ = first + second
return summ
print summ

I have considered only a two digit no., to start with but has threw up the following error:

File "python", line 3
second = num%10 #if no. is 21, modulo gives 1
^
SyntaxError: invalid syntax

and

Oops, try again. Did you create a function called digit_sum? Your code threw a "global name 'digit_sum' is not defined" error.

Could you let me know where m wrong.


#6

You forgot another parenthesis at the end of that line of code.


#7

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