Digit_sum help - how to separated characters in a list


#1

I've been searching for a way to solve this for an hour, and I'm getting really discouraged. :frowning:

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

I'm getting this error:
"Traceback (most recent call last):
File "python", line 14, in
File "python", line 3, in digit_sum
TypeError: 'int' object is not iterable"

and this:
"Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "'int' object is not iterable" error."

What I want to do is be able to separate every letter/number in n[], even if they are not already separated (ex: '143' would be '1', '4', '3', not '143'). and add them together, x. Ideas for separating each item in a list? Something like n[0:] or something?

Thank you in advance. :slight_smile:

def digit_sum(n):
    n == []
    for num in n:
        x == 0
        if num % 1 == 0:
            x += num
        elif num == str('num'):
            int(num)
            x += num
        else:
            print 'error'

edit: I realize there is a lot wrong with my code, I'm still learning.


#2

here:

n == []

why use two equal signs? this means, check if equal. This is usually something you do in a if condition. To name a variable n and give it a empty list as value, use a single equal sign

then the error you get, which is here:

for num in n:

n is an integer. Integers are not iterable. You could convert to string? Strings are iterable


#3

Thank you! That makes sense... silly mistakes.

I changed it a bit but am still getting errors.

This is the new code:

def digit_sum(num):
num = []
for i in num: #for each item in the list..
x = 0
if i % 1 == 0: #if i is indeed an integer..
if i > -10 and i < 10: # & a single digit..
x +=i #add to the growing sum
else:
digits = [int(d) for d in str(i)]
x += digits #form a str, break str, sum
else:
print('error')

(I don't know how to print this with the indentations, sorry.)

This is the error message:

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

Any ideas? :confused:


#4

I scratched it and started over. I looked through the FAQ and found something that, when I wrote it out on paper, worked wonderfully. So I tried to write it in my own way and something is off, but I'm not sure what.

def digit_sum(n):
    digits = []
    for n in digits:
        sum = 0
        while n > 0:
            remainder = n % 10
            sum = remainder + sum
            n = n // 10
        return n
        return sum

This is the error I'm getting:

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

Yet if I do the math on paper, using the steps in my function, I get 11, so maybe its formatting again?

Any ideas would be very appreciated.


#5

your latest approach doesn't require a for loop.

The while loop alone is enough. Get rid of the for loop, and you should be good.

given n = n // 10 will eventually make n zero, the loop will become false

a single digit (1-9) divided by 10 (and then floored) will be zero


#6

Thanks. I took the for loop out but am still getting the same error message.

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

#7

pretty close now, a function ends the moment a return keyword is reached

so you should return sum, not n


#8

Why do we return sum instead of n?


#9

because sum contains the total/sum of n, and we need to return that, that was the point of the exercise? To write a function which returns the digits sum, which you stored in sum variable


#10

A post was split to a new topic: Digit sum


#11

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