# Digit_sum kept returning None

#1

I'm doing the digit_sum exercise on the Practice makes perfect section of the Python course in Codecademy

I'm getting the message:

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

def digit_sum(n):
added_number = 0
for i in str(n):
str(i).split()
added_number += int(i)
return added_number

number = raw_input("Enter your chosen number here: ")

digit_sum(number)

When I run the code, it prompts for the user input. I enter: 1234

This is the output:

Enter your chosen number here: 1234
None

I have no idea what I'm doing wrong. I don't see where 4 is returned. Please help.

#2

A function ends the moment a return keyword is reached

a return keyword is reached in the first run/iteration of your loop, so your function ends in the first iteration of the loop. That can't be right, the whole loop needs to run to get the digits sum

#3

Simplify the problem by not drawing user input into the equation. Use a direct call with a literal.

``print digit_sum(1234)``

You are the user, nobody else. Once we pass these introductory lessons is the time to review, research and refine.

#4

So in other words it needs to loop before it is returned?

#5

how would I rewrite this code to make the function iterate through the entire loop?

#6

by placing return outside the loop

#7

I took your advice and changed it. Thanks.

#8

Oh wow! It worked! I guess I'm still getting used to the spacing in Python. Thank you so much!

#9

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