Practice makes perfect: digit sum


#1



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


I keep thinking that my code should take the argument number and convert it to a string and store it in the variable numbers. Then I used a for loop to go through each string(s) in the variable numbers and then convert it to a integer and then each integer(s) + the next integer and then return the result. Somehow it equals 44 and not 11 when the computer tests it on "434". I guess I don't understand why it does that and what I am doing wrong?


def digit_sum(number):
   numbers = str(number)
   for s in numbers:
       int(s)
       result = s + s
       return result


#2

i recommend to declare result outside the for loop and give it a value of zero.

then add int(s) to total in the for loop

a function ends the moment a return keyword is reached, this is currently in the first iteration

place return outside the loop

if you need more help post an updated version of your code


#3

Ok I got it. I think I understand how this works, can you let me know if Im off or wrong?

my code:

def digit_sum(number):
   numbers = str(number)
   result = 0
   for s in numbers:
       total = int(s)
       result += total
   return result

so this is how I understand it, it takes whatever argument put into the function, then it turns it into a string and adds it to the variable numbers. Result = 0, and then the for loop goes through each str in numbers, total converts each string to an integer and then the second "result" adds each one of those converted integers back into the first result, essentially (for example) getting s(4) + s(3) + s(4) = 11. Am I right?


#4

yes, you are right :slight_smile:

Well done! :slight_smile:


#5

Is it normal for a beginner to be struggling a little on this practice makes perfect? Or should I really have this stuff down pat before I even got to this section? I can get majority of the code and sometimes the code itself with maybe something out of place here or there. lol


#6

Hey - this is actually super helpful as I was stuck on the same exercise - it's really cool to see how other people think :blush:


#7

Yes it definitely is. Each problem seems to have multiple solutions and its pretty cool seeing how each person arrives at the solution.


#8

yes, this is perfectly normal. This exercises push you, which is a good thing, i can take your coding to a next level.

Codecademy focuses on coding, this few exercise are more programming (problem solving, thinking in steps), so struggle is normal, struggle means you make progress

i can recommend to readwikipedia pseudocode, to write a bit of pseudo code in this exercises would be good.

Reflect on what you have learned, revisit old exercises, check documentation.

I am sure that if you come back here later after completing the python track on codecademy you can optimize the solution :slight_smile:


#9

Thankyou. I wish I could spend more time on each exercise without looking towards the q & a fourms for hints, but im afraid of taking to long and them taking the Python course down like they said they would by the end of year. Not sure when thats suppose to happen, but I think they said Q4. Thanks for the advice too


#10

If you want challenges, head over to codewars, warning: the challenges there are challenging. After completing a challenge you can see other peoples solution, quit useful, but don't be surprise if for digit sum you will see a solution like:

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

there are people on codewars who have been coding for a while


#11

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