# Does your code take one argument?

#1

I understand for this lesson there may be multiple correct answers. In regards to the error code, I only have one "for" argument in my function, so, "yes"? & Im not sure if I am converting the numbers correctly but can't find answers from previous lessons. Some of the error messages haven't always been that helpful and most times it has been an "indentation" error. Can someone help walk me through this code and set me straight?

Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "unsupported operand type(s) for +: 'int' and 'str'" error.

Also, they should post these forums by lesson and error code. Just a thought.

``````def digit_sum(n):# creates fucntion
string = str(n)#converts n into a string
total = 0 #initiate variable
for x in string:#iterate through the string
total = total + x#adding all the contents of the string
return total# returning the sum of the numbers in the string``````

#2

this line:

``total = total + x``

total is a integer, `x` is a string. Now look at the error message. You will need to cast `x` to a integer

Also, a function ends the moment a return keyword is reached, which in your case, happens in the first run of the loop, you might want to change the indent of return so the whole loop can run

#3

Thank you for the help, however I think I need to ask better questions. Could you provide insight into where this conversion should take in the code? Im not sure if my conversion code is correct but I think understanding where it should be could help me out!

#4

you want to add x (string) to total (integer), which doesn't work (different data types)

you have two options, casting x to integer (`int()`) or cast total to string (`str()`)

lets look at the following code (using strings):

``````a = "1"
b = "2"
c = "3"
d = a + b + c
print d # which will output: 123``````

and the other code (using integers):

``````a = 1
b = 2
c = 3
d = a + b + c
print d # will output 6``````

now, armed with this new knowledge, what would be the next logic step?

#5

def digit_sum(n):# creates fucntion
()string = str(digit_sum)#converts n into a string
()total = 0 #initiate variable
()for x in string:#iterate through the string
()()x = int(string)# converts each iteration into an integer
()()total = total + x #adding all the contents of the string
()return total# returning the sum of the numbers in the string

#6

this line:

``x = int(string)# converts each iteration into an integer``

your comment is false, this will convert your entire string into a integer again, you just want to do this to each individual element (`x`) in the string

Please read this topic to display indent:

#7

I'm not sure what to replace "string" with. I originally placed the code: `x = int(x)` with the thought each iteration would now become an integer.

I then receive the error: Your code looks a bit off--it threw a "invalid literal for int() with base 10: '<'" error.

I'm thinking there must be a very minor error in the existing code as the only step left is to convert x into an integer and I must convert x into an integer before adding it with total. Is the content between the int() brackets the error in the code?

#8

also thank you for the tip, the existing code looks as follows:

``````def digit_sum(n):# creates fucntion
string = str(digit_sum)#converts n into a string
total = 0 #initiate variable
for x in string:
x = int(x)# converts each iteration into an integer
total = total + x #adding all the contents of the string
return total# returning the sum of the numbers in the string``````

#9

this code looks fine, do you understand it all now?

#10

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