# Practice Makes Perfect 4/15 why am I getting error?

#1

Why am I getting error, when my code does what it is supposed to do, I get my result, 10, printed on the screen, but it tells me I have an error and doesnt let me continue to the next part???

Here is my code:

``````def digit_sum(n):
n = str(n)
one = n[:1]
two = n[1:2]
three = n[2:3]
four = n[3:4]
n = int(one) + int(two) + int(three) + int(four)
return n
print digit_sum(1234)
``````

I tried for hours to use a for loop instead, but Im doing something wrong…and when I look at the Codecademy solution to see how they did it, they gives the second(hard) solution…

Can you help me with the simpler solution they are asking for, where they aks us to convert the integer to a string with str(), iterate over it, and turn the substrings back into integers with int() to do the addition.

#2

digit_sum should also be able to handle digits of multiple lengths, so this:

``````print digit_sum(1234)
print digit_sum(12345)
print digit_sum(123456)
print digit_sum(1234567)
``````

should all be possible, which is why you should use a loop.

Start by looping over `n`, you know how to do that, right?

#3

for number in n:
print number

I’ve tried for hours with the for loop, but the only thing showing on the screen is the first number.
Thats why I need help

#4

now before the loop, we need to create a variable named `total` and give it a value of zero. Then we can add all values to total using the loop

don’t forgot that `number` is of type string, so you need to cast to integer before adding it to total

that would be helpful to mention in your first post, please show the full code of where this problem is occurring.

#5

I deleted it when I wanted to look at the codecademy solution, but let me write it again…back in few min

#7

I think it was something of that nature…

``````
def digit_sum(n):
n = str(n)
for num in n:
total = 0
total.append(num)
total = sum(int(total))
print digit_sum(1234)
``````

#8

you should know by now that after a loop additional indent is required, you do it correctly here:

``````for number in n:
print number
``````

but here:

``````  for num in n:
total = 0 # indent missing before total
``````

not. Confusing

`total` should be defined before the loop, otherwise it will just re-assign zero each iteration of the loop, overwriting what you add to `total`

`.append()` is a method to insert at the end of a list. `total` is not a list.

`sum()` is a built-function to get the sum of an iterable, like a list.

its a possible solution to append all values to a list and then use sum, if that is what you want.

You need to think about what certain methods (append) and function (sum) do, and for what data-types, and if it fits your solution

#9

The missing indent was my bad, i didnt notice that, just a littel stressed…

Ok so where and how can I store the string characters, i tried doing the total = " ", but id didnt work either… because I thought the " " is when you want to store a string, or am I mistaken again?

I want to store them then turn each character back into integers, and add them together (with add() I guess)…

#10

sure, you can cast `num` to integer and store it in a variable, but you can also cast `num` to an integer and add `int(num)` directly to total.

we can add integers together like we have always done in math using the plus operator.

also, don’t forgot we want to update `total` with the added value.

#11

Yes, thats what I did in my first code:

n = int(one) + int(two) + int(three) + int(four)

but, that didnt do anything, i got the result 10 on the screen, but error from codecademy…so Im pretty lost considering my for loop is also giving me trouble…

#12

i showed you why that is a bad idea? You don’t know the number of digits.

Which is why we use a loop. Each iteration of the loop we add `num` (containing current digit) to `total`. So that our function can handle any number of digits

#13

Right, of course…but when i turn the number into a string like this:

n = str(n)

and then try to loop throught the character of the strings like this:

for char in n:

I dont know how to store those characters…

I can print them yes, 1 by 1 like this:

for char in n:
print char

But when I start adding variables where I can store the characters etc, things go south
I need to know where and how to store the characters, so that I can later turn them into integers and add them together

#14

that shouldn’t be a problem, right:

``````for char in n:
c = int(char)
``````

There are two options, what suggest requires a list defined before the loop, so you can `.append()` all the numbers

what i suggest is add each `char` (`num`, whatever variable name you use) to `total` each iteration of the loop.

#15

def digit_sum(n):
n = str(n)
total = [] <— Like this?
for num in n:

``````total.append(num)
total =
``````

print digit_sum(1234)

#16

yea, that is possible. don’t forget to cast `num` to integer before appending it to the list

then after the loop we have a list with all the digits (as integers), so we can use `sum()` if you really want.

#17

def digit_sum(n):
n = str(n)
total = []
for num in n:

``````total.append(int(num)) <---?
total = sum(total) <---?
``````

print digit_sum(1234)

#18

`total.append(int(num)`, pay attention to your parentheses.

sum calculates the sum of a list of integers, when do we want to do this? After the loop, after we successfully appended all digits to the list.

If we do sum during the loop (like you currently do), you will get an error. Given `sum()` will return an integer, total will become an integer, and integers do not have an append method (methods belong to specific data types)

#19
``````def digit_sum(n):
n = str(n)
total = []
for num in n:

total.append(int(num))

return sum(total)  <--- is that what you mean?...because the code worked :D or is there a better alternative?
print digit_sum(1234)``````

#20

but do you understand why it works?

i would have done `total = 0` and then add `num` to total each iteration of the loop. Given then we would only have a string and integers. Your solution has string, integers and list. Plus sum() very likely uses another loop under the hood. So your solution has “two loops”

even better would be the proposed solution by the exercise, which only involves integers and math, no string/integer casting, but that solution is more complex.

#21

I get what the code does…it adds each character from the string in to the list called total, but before it reaches the list, the character is turned into an integer. When all the characters are added to the list, they are summed up in the final line return sum(total)

But I thought putting total = 0 inside the loop was wrong?!

def digit_sum(n):
n = str(n)
for num in n:
total = 0 <—???

``````total.append(int(num))
``````

return sum(total)

print digit_sum(1234)

Yes thats too complex for me right now…