Digit sum (4 in practice make perfect) variable error ?!


#1

what is wrong here ??! and how did you answer that exercise !! My head turns over ..please help me


#2

looks like there is a space after digit_sum and before the input ().

it should look like:

def digit_sum(n):


#3

didn't solve it :frowning: :(


#4

Ah the only other thing i can see is it looks like the tab or indents on the lines below aren't correct.


#5

After few tries (not solved ) :frowning:


#6

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.


#7

no difference :frowning: thanks for your patience with me


#8

no worries :wink:

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

#9

@webrockstar49026
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.


#10

@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.


#11

@webrockstar49026

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.


#12

finally solved thanks alot guys :grinning::grin:
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

#13

@coderockstar13753

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.


#14

Hi, bro:
def digit_sum(n):
a = 0
for i in str(n):
a += int(i)
return a