what is wrong here ??! and how did you answer that exercise !! My head turns over ..please help me
looks like there is a space after digit_sum and before the input ().
it should look like:
Ah the only other thing i can see is it looks like the tab or indents on the lines below aren't correct.
right now your code is correct as far as formatting anyway.
Highlight your code and copy it.
close the tab and reload the website.
delete the code and paste in what you copied.
This has happened to me a few times, i think there might be a bug on the site.
no difference thanks for your patience with me
just use this! but let me know if you don't understand any of it.
def digit_sum(n): n = str(n) n_lst =  for char in n: n_lst.append(char) total = 0 for char in n_lst: num = int(char) total += num return total
Please refrain from posting the answer it really fails to help anyone, as to why @coderockstar13753 your code is not working is that you are referencing a variable called
suma before assignment. This means you never assigned a value to it.
def digit_sum(n): suma == 0 # This is not how you assign to a variable z = str(n) for i in z: m = int(i) suma += m return suma # This line will not work out, once a return is processed the function ends.
Take a gander at what I put and get back to me if you still need help.
@zeziba like i mentioned the code was correct as far as formatting. but i could see there was an error in the code. however the error he was getting was saying he hadn't created the function.
The reason for that is because he has bad code inside his function, so as python was going over, it threw an error which will do stuff like that. That is why you need to know a little more about coding because sometimes the error messages can be confusing.
finally solved thanks alot guys
but I don't get the (((return))) .. how to use it .. when to write and when not .. and why didn't it work with the previous indentation??????
def digit_sum(n): suma = 0 z=str(n) for i in z: m = int(i) suma += m return suma
The reason why it didn't work at the previous indentation level is because as I stated before when you ht a return statement it returns there is no more go in the function or loop or what ever you were doing. It gives back the data and stops everything.
Moving on, you should be able to see that you can simplify your solution quite a bit, we will start by removing redundant code and then give a cool one liner for you to look at.
EXAMPLE:Removed redundant code
def digit_sum(number): sum_ = 0 for digit in str(number): sum_ += int(digit) return sum_
As you can see above we can simply just add most of the extra lines straight into the for loop and that drops our line count down and makes it easier to see whats going on. Next let's write that one liner for you to look at.
EXAMPLE: One-liner Digit-Sum
def digit_sum(number): return sum(int(digit) for digit in str(number))
If you study this you will see all the same components that you have in your function, and even the adding function you had just replaced by the built-in function. This is called a generator expression, and it they are generally faster than comparable for loops because python converts the code into byte-code which runs tons faster.
You generally have to compare your generator to a for-loop to get your real results because as I said it is generally faster and if speed is important you want the best solution for each problem. For this it matters not because both get completed in a fraction of a second any how.
a = 0
for i in str(n):
a += int(i)