Stuck on digit_sum


#1


Hi, I'd kind of like your help please because I'm stuck here --> https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#
...and I don't necessarily know what is going on or what the instructions are asking me or how the code works or is supposed to work. I don't really get the logic of python for the past many, many lessons that I've done it so in essence - I literally have no clue as to what I am doing. (If anyone knows where I can go I to understand what is going on a bit better then I'll be super grateful.)

Here is my code.....


n = int(input("Type in a number of your choosing: ")
def digit_sum(n):
    for i in n:
        total += int(i)
    print total

Thank you for your help, I cannot thank you enough. :blush:


#2

you can't loop over a integer, maybe in the for loop change n to a string (str() since you can loop over strings?


#3

thanks for your help so far - it's working a bit better now, but I still am running into problems.

here is the modified code:

number = int(input("Type in a number of your choosing: "))
def digit_sum(number):
     for n in str(number):
         total = 0
         total += int(number)
     return total

and in the console I got this after running it:

Type in a number of your choosing: 434
None

and I also got this error message:

Oops, try again. Your function fails on digit_sum(434). It returns 434 when it should return 11.

... So what exactly is going on?


#4

Hello I'm a student like you but maybe i got this:
seems there are 2 errors on it:
-1 you are adding the whole number in total and not the single digits
-2 are you sure that total=0 should stay inside the for? so every time it goes the loop your total is 0

Sorry for my bad english i hope it helped


#5

Uuh I tried what you said and the console threw an error saying total referenced before assignment, so I'm stuck atm and I don't know how to split a number up into single digits.... :sweat:


#6

I agree with you, total=0 shouldn't placed inside the for loop


#7

can we see an updated version of your code?


#8

Here it is

number = int(input("Type in a number of your choosing: "))

def digit_sum(number):

for n in str(number):

total += int(n)

return total


#9

Wait a min.... I've looked at another person's code and altered my own...

it says this:

def digit_sum(number):
        numbers = str(number)
        result = 0
for n in str(numbers):
        total = int(n)
        result += total
        return result

And it's still saying that return on the 7th line is outside the function, and there is also a global error thing that says my function is not defined....


#10

check the indention of your code, the for loop should be indented in such a way that it is inside your for loop, then total and result += total should be indented in such a way that they are inside your for loop


#11

Got it now, thanks for your help :slight_smile:
Is it normal to encounter difficulties like this?


#13

Yes, it is. But you can learn from it to become a better programmer :slight_smile:


#14

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