4. digit_sum working just fine but cademy system isn't letting it pass!


#1

Try summing the digits of a number.

I did it like this:

def digit_sum(n):
n = abs(n)
n = str(n)
count = 0
for i in n:
i = int(i)
count = count + i
print count

digit_sum(434)

For 434 it DOES return 11 but the error says:

Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11.

What's happening?


#2

Solved it!

Replace "print count" with "return count".

But......WHY?


#3

because of instructions?
For example: digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4


#4

Yes, I understand but why only return and why not print?


#5

because the exercise wants it? You can also print it, and then return it on the next line.

Return and and print do different things, but that should already have been explained, return actually gives something back, so when you can call the function:

store = digit_sum(434)
print store

you can actually store the result in a variable. Where print only prints it


#6

Print and return are very different functions. When we define functions, we often won't need them to print anything. They will be called so we can perform a function and get some value back that will be used in the code elsewhere. They won't have to print.

Print is used to literally show the returned value. But if you print instead of return, the function isn't giving the answer back.

Imagine you told your employee to go count a stock item and write it down on a piece of paper. So they do. And then they come back and you say, "how many items were there?" And the employee says, "I don't know, I've already forgotten. I wrote it down on a piece of paper that's back in there."

That wouldn't be very useful for you. Good for anyone who goes into that stock room and looks at the piece of paper, but the employee didn't come back with anything. They just printed the answer out.

But if you tell the employee to go count the items and then return with the result, they won't write it down (so no one will see it), but they will come back with an answer.

So return brings back an answer you can use.

Whenever you call a function, you can always do it with the print statement, which will print out whatever the function returns - even if the function itself doesn't do any printing:

print digit_sum(1234)

That would print out the returned value from the function. So it would print "10" on your screen. Both are useful functions, but need to be separate.

I pretty much just wrote this up so I could explain it to myself. You learn when you try and teach someone else. :slight_smile:


#7

I used your code and tried to modify it as follows:
my modified code:

def digit_sum(n):
    n = abs(n)
    n = str(n)
    count = 0
    for i in n:
        i = int(i)
        count = count + i
    return count ```
``` # I first moved print out of for loop, this gives me only the final summation but before, it returns each single integer added to count. Then I replaced print with return and passed the code
        
print digit_sum(-434)
print digit_sum (1234)

#8

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