4. digit_sum logically I think im right


#1



The function isn't adding what I thought was being appended to my list


I have commented out my code, but I was expecting to simply take each integer, change it to a string and add to a list. Then simultaneously change each entry to an integer and add them together.


# Defining a list to place each integer in
num_list = []


def digit_sum(n):
    # Take a number and turn each int into a str
    # While appending them to the list 
    num_list.append(str(n))
    total = 0
    
    # Parsing the list and changing each entry to an int
    # While adding them together
    for each in num_list:
        total += int(each)
    return total   
    
print num_list # visual check of num_list
print "Sum of the number is", total


#2

Did you call your digit_sum function and parse data to it?
something like this outside function at the end.

if that is all your code, then you need to call you function.

print digit_sum(1234)

and this line:

num_list.append(str(n))

It will change the integer to string. if you call digit_sum(1234), num_list will look like this: ['1234']. but what do you want is ['1','2',....]


#3

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