4. digit_sum


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


I get this error message:
Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help


def digit_sum(n):
    x = str(n)
    a = len(x)
    result = 0
    while a > 0:
        y = int(x[a])
        result += y
        a -= 1
    return result


#2

You are overthinking this. Try following this template:

def FUNCTION(PARAMETER):
    VARIABLE = 0
    for INDIVIDUAL_ITEM in str(PARAMETER):
        VARIABLE += int(INDIVIDUAL_ITEM)
    return VARIABLE

Make sure not to just follow the template and move on. I want to make sure that you understand how and why this works. If you have any questions, just ask.


#3

Oh wow. I interpreted way too much into this. Thanks a lot, I got it now!


#5

I would be grateful if you could expand on this. I'm still not getting it. Sorry im struggling to think like a coder.

Why do you not have to set individual_item to 0 like you do for variable?


#6

To understand why we use INDIVIDUAL_ITEM the way we do, you must first understand why we use parameters.

As of defining the function, the program does not know whether PARAMETER is a number, a string, a list, a dictionary, etc. As far as the program is concerned PARAMETER could be anything and, well, it could care less. It's just a placeholder for whatever you pass into the function when you actually call it. In other words think of parameters like variables. Whenever you call a function, the argument you pass into it becomes the value for PARAMETER. For instance if you called:

FUNCTION(122)

You would essentially be telling the program to run FUNCTION(PARAMETER) except now, PARAMETER = 122. Until a function is actually called, the parameters you have set are valueless placeholders. Once you call the function and pass in arguments for those parameters, they take on value and purpose. What I mean by this, is if you did the following:

def FUNCTION(PARAMETER):
    VARIABLE = 0
    for INDIVIDUAL_ITEM in str(PARAMETER):
        VARIABLE += int(INDIVIDUAL_ITEM)
    return VARIABLE

FUNCTION(122)

Notice how I defined the function using PARAMETER as a parameter and then I called the function using 122 as an argument. This is telling the program to run the code within FUNCTION(PARAMETER) but replace all mentions of PARAMETER with 122. Basically it does this:

VARIABLE = 0
for INDIVIDUAL_ITEM in str(122):
    VARIABLE += int(INDIVIDUAL_ITEM)
return VARIABLE

INDIVIDUAL_ITEM is another form of parameter, in that it is a dynamic value. Each time the code within the for loop is run, INDIVIDUAL_ITEM equals something different. If we were to set it equal to something, it would become a static variable and would therefore be useless to us in our operations.

This is why we use INDIVIDUAL_ITEM as we do in the function.


#7

I am very grateful for your detailed explanation. This makes complete sense now. Copied and saved this answer to my notes, so i can refer back to it again.


#8

Interesting take on this and it indeed shows that there are so many ways to resolve the same exercise.
When I first saw this I had a throwback to school when we were coding maths and logical problems using Pascal.

One of the challenges was to count the number of coins needed for a given sum of money. For example, how many €1, 50c, 20c, 10c, 5c, 2c and 1c are needed to make up €3.88.

My approach was to find a pattern in isolating the individual digits and include that in a while loop.

The pattern is:

1: Isolate the last digit (number % 10)
2: Add it ti the total
3: Chop the last digit off the number (number // 10)

This way, the number will eventually become zero, which gives us the exit condition out of the while loop

def digit_sum(n):
    total = 0
    while n > 0:
        total += n % 10
        n //= 10
    return total

#9

So did I get it right? You turn the Parameter into a string, because you cant go trough an integer with the for-loop? Then you say every individual_item of your string should be chanced into integer that you can sum it up? I hope one day I will start thinking like a coder...practice practice practice


#11

some thing i didn't get it why u convert parameter to string then convert it to int ?


#12

This topic is becoming a clutter, and the topic has been solved so it will be closed

if questions persist, please make a new (linked) topic

@omars.ibrahim because integers aren't iterable so you convert to string so you can use a loop, even better would be the solutions which do not involve string/integer conversion, given they use math only, which is more efficient


#13