4/15


#1

For some reason I cannot get my code:

def digit_sum(n):
    total = 0
    x = str(n)
    for y in x:
        total += int(y)
    print total

to return the correct answer (ex. n = 434, total = 11). It returns None.
However, I can get:

n= 434
total = 0
x = str(n)
for y in x:
    total += int(y)
print total

to return total = 11

Is there something I am missing here?


#2

You want a function to "return" a value rather than print it:

def digit_sum(n):
    total = 0
    x = str(n)
    for y in x:
        total += int(y)
    return total

print digit_num(434)    # will return 11 as expected :)

#3

... I'm an idiot. Thank you.


#4

There... fixed that for you :smile:

I meant to switch "idiot" for "learner" because I feel you shouldn't let anyone, least of all yourself, call you an idiot even in jest.

We're all learners here and learning is by definition the action of transforming mistakes into knowledge.