# Stuck in digit_sum!

#1

def digit_sum(int(n)):
return xx=str(int(n))
for char in xx:
return int(char)

INSTRUCTIONS:
.
Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number’s digits. For example: digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4. (Assume that the number you are given will always be positive.)

ERROR TYPE:
File “python”, line 1
def digit_sum(int(n)):
^
SyntaxError: invalid syntax

#2

This is an invalid return statement, for starters, but it also should not be the first line of the function.

Assuming the earlier line is removed, this line will only return a single digit, not the sum of the digits.

#3

so i have to remove the return xx=str(int(n))?

and then what i have to do with the for loop?

#4

Remove the return statement and instead add the value to a running total.

``````total = 0
for char in str(n):
total += int(char)
``````

When the loop is finished, then will the time to return the total, but be sure the statement is not inside the loop block.

Also, and perhaps more importantly, we cannot call a function in the parameter list.

``````def digit_sum(n):
``````

The time to apply a function would be in the call argument,

``````print digit_sum(int(n))
``````

where it is permissible. A call argument may be any expression, including a function call.

#5

def digit_sum(n):
xx=str(int(n))
total=0
for char in str(n):
total+= int(char)
print digit_sum(int(n))

THIS DOESN´T WORKS

def digit_sum(n):

total=0
for char in str(n):
total+= int(char)
print digit_sum(int(n))

THIS NEITHER

#6

Doesn’t work, or does not pass?

``````>>> n = 123456
>>> total = 0
>>> for char in str(n):
total += int(char)

>>> total
21
>>>
``````

Please post a link to the exercise so we can test your code. Thanks.

#7

But you´re storing the number 123456 to n when it´s suppposed to be a variable given by the user, in fact, i got an error type that says something related to that
:
Your function fails on digit_sum(434). It returns None when it should return 11.

HERE´S THE LINK OF THE EXERCISE

def digit_sum(n):
n=123456
total = 0
for char in str(n):
total += int(char)

#8

I was testing without using a function. The test was done in the interactive shell.

Do no reset `n` inside your function.

#9

i tried without reseting it and i got the same

#10

this item is very hard

#11

You don’t have to convert `n` to an `int`. It is expected it will be one.

``````def digit_sum(n):
total = 0
for x in str(n):
total += int(x)

print digit_sum(123456)
``````

Study the above code and see where yours needs to change. Tested and passes.

#12

thanks, one more question. Why i have to return the total in order to call the function after defining it?

#13

`return` lets us pass data back to the caller. Because of the different scopes, the caller cannot see the total inside the function, hence we return it so it can be printed.

#14

got it! i don´t know if someday i´ll get a decent level of programming, seems very hard at least learning on the internet how to do it

#15

You will if you stick with it. If it was easy, everybody would be doing it and the wages would be minimum. Don’t set a time limit on learning. Work at it on a regular basis, practice, review, read, experiment. Build on your skills as you go, but don’t try to hurry, and don’t be afraid to make mistakes or hit a wall. That is going to happen to everybody. The ones who bounce back and refuse to quit are the ones who succeed in the long run.

#16

im in the correct path if im trying to learn python? or i should learn another language?

#17

When asked this question I almost always suggest, HTML, CSS and JavaScript as the starting point. JavaScript is a very powerful language, today, and it is built into every browser so you don’t need any software. Text editor and browser. That’s it.

I find that people with JavaScript in their background have no trouble learning Python, PHP, Ruby, Java, etc. It might be worth a try. Give yourself a couple of months to learn the above three API’s, starting with HTML. Read and research while you learn, even if it seems to take forever.

#18

so you recommend to drop python? i´ve been working on it since a while, will be a pitty to drop it

#19

No need to drop Python. This is a basic introductory course so go ahead and finish the track. When you reach that point, jump into the HTML/CSS/JavaScript path.

There is no sin in putting a course on hold, or slowing the pace so you can embark on other courseware. Since it is a good idea to have some HTML and CSS under one’s belt before learning JavaScript, while you complete the Python course track, set aside time to engage in the HTML track. There is a great enough difference between the two languages (they are nothing alike) that you will be learning with no conflicts or overlaps.

#20

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