15. Practice Makes Perfect


#1

Hi!

I’m stuck on this exercise. I have made the following code

def digit_sum(n):
  string = str(n)
  for i in string:
    list = []
    list.append(int(i))
    sum1 = sum(list)
    return sum1

and recieve the following error message and cannot seem to find the error:

“Your function fails on digit_sum(434). It returns 4 when it should return 11.”

Thankful for your help!


#2

Here:

    for i in string:
        list = []
        list.append(int(i))
        sum1 = sum(list)
        return sum1

A function ends the moment a return key is reached. In other words, the return statement should be outside the loop (on the same indentation level as the for statement itself).


#3

Thank you for noticing the error! However, the same error message is still returned.


#4

Here:

    for i in string:
        list = []
        list.append(int(i))
        sum1 = sum(list)
    return sum1

The line list = [] should come before the for loop…

    list = []
    for i in string:
        list.append(int(i))
        sum1 = sum(list)
    return sum1

As it is right now, list gets redefined as an empty array ([]) at the start of each iteration. In doing so, whatever was previously appended to list gets deleted. By defining list outside of the for loop, we can freely append items to list without worrying about them getting deleted.


#5

That did the trick! Thank you!


#6

I tried to do it “the challenging way” according to the tip.

If you’re looking for a challenge, try this: to get the rightmost digit of a number, you can modulo (%) the number by 10. To remove the rightmost digit you can floor divide (//) the number by 10. (Don’t worry if you’re not familiar with floor division—you can look up the documentation here. Remember, this is a challenge!)

I want to find a way to use the “peeled” number each loop, but I’m not sure if I’m accomplishing that.

def digit_sum(n):
  string = str(n)
  l = len(string)
  list1 = []
  for n in range(l):
    x = n % 10
    list1.append(x)
    x = n // 10
  return sum(list1)

I would be very thankful for your suggestions.


#7

I’m not sure if this is what you are wanting, but perhaps this template could help:

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

#8

I apologize for the poor explanation.

I added the description of what they call a more challenging way to complete the task:

If you’re looking for a challenge, try this: to get the rightmost digit of a number, you can modulo (%) the number by 10. To remove the rightmost digit you can floor divide (//) the number by 10. (Don’t worry if you’re not familiar with floor division—you can look up the documentation here. Remember, this is a challenge!)

I’m trying to figure out how to do this. Right now I’m sitting with something like this

def digit_sum(n):
  l = len(str(n))
  list1 = []
  c = 0
  for n in range(l-1):
    x = n // 10**(c)
    x = n % 10**(c+1)
    list1.append(x)
    c += 1
  return sum(list1)

I receive the following error message:

Your function fails on digit_sum(434). It returns 1 when it should return 11.


#9

Alright, Try following this template:

def FUNCTION(PARAMETER):
    ARRAY = []
    VARIABLE_ONE = 0
    for INDIVIDUAL_ITEM in str(PARAMETER):
        VARIABLE_TWO = int(INDIVIDUAL_ITEM) % 10 ** (VARIABLE_ONE + 1)
        ARRAY.append(VARIABLE_TWO)
        VARIABLE_ONE += 1
    return sum(ARRAY)

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.


#10

I made it work with the following:

def digit_sum(n):
  var = n
  l = len(str(n))
  list1 = []
  c = 0
  for i in range(l):
    i = var
    y = i // 10**(c)
    x = y % 10
    list1.append(x)
    c += 1
  return sum(list1)

alternatively

def digit_sum(n):
  l = len(str(n))
  list1 = []
  for x in range(l):
    x = n % 10
    list1.append(x)
    n = n // 10
  return sum(list1)

I saw another poster that did a much more elegant solution however.

I’m not sure if I understand your code. How can it still work without using floor division, and only using modulo? For example, 434 % 100 is going to be 34.


#11

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