4. Digit sum


#1

Task:
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.)

Check the hint if you need help!

Hint:
One way might be to convert the integer to a string with str(), divide it up, and turn the substrings back into integers with int() to do the addition.

If you're looking for a challenge, try this: to get the rightmost digit of a number, you can modulo (%) the number by 10. To remove the rightmost digit you can floor divide (//) the number by 10. (Don't worry if you're not familiar with floor division—you can look up the documentation here. Remember, this is a challenge!)

Try working this into a pattern to isolate all of the digits and add them to a total.

My question. How to
One way might be to convert the integer to a string with str(), divide it up, and turn the substrings back into integers with int() to do the addition.

divide it up

How exactly do that?


#2

You want to see the solution? but I need to know if you solved it or not?
Ps: there are more than two methods you mentioned here.(I mean many) :slight_smile:

Further hint**

My question. How to

Well, lets say I have a number 1234
But instead of adding them I want multiplication of all of its digits..

Key things/concepts I will use:

  1. Interpolation methods: str() , int()
  2. Loops
  3. Global variable creation
  4. use of reassignment of variables

x = 1234

x = str(1234)  # type change
multi = 1 # global value creation
for digit in x: #loops
    multi = multi * int(digit)  # interpolation  + reassignment

print multi

do you get the idea?


#3

Actually, no.

My code:

def digit_sum(n):
    for a in str(n):
        return a
    
print digit_sum(1234)

Nothing useful happens.
Error:
Oops, try again. Your function fails on digit_sum(434). It returns 4 when it should return 11.


#4

def digit_sum(n):
    for a in str(n):
        return a

Lets read your code what it does and what it had to do..

  1. First line defines a function named digit_sum that takes string number as a
    parameter.
  2. You are iterating over string value of n (str(n))...
  3. Inside body of for loop:

a takes first character from str(n) and returns it.

Function halted

When code inside function hits return, it just says "Okay stop execution here and take me out of function, even there is code after it . Just halt it."

you're making mistake there.
Take a look at my example once again and try to find what error you observe inside your code. They're exactly having similar approach!


#5

def digit_sum(n):
    # Replace this comment with a result variable, initialize it to 0    
    for a in str(n):
        # Here, reset the result variable, like so: result += int(a)        
    # Return result. Pay attention to the indentation

That works for non-negative integers. Hope that helps.


#8

When i took Your code and try to run it i got error:

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


#11

Guys, i got it, that I can't understand this lesson, so just please give me right code and i'll go further.


#12

It was not my code but yours that I was showing why It was not working for you! :slight_smile:
Ps: If I said to check my first post , In that code I gave a recipe to find multiplication of digits of a number!


#13

x = 1234

x = str(1234)  # type change
multi = 1 # global value creation
for digit in x: #loops
    multi = multi * int(digit)  # interpolation  + reassignment

print multi

This isn't my code, sir.


#14

where is your function? If you need help, please post an updated version of your code.

Posting or asking for answers is not according to the guidelines


#15

But instead of adding them I want multiplication of all of its digits..

Key things/concepts I will use:

Interpolation methods: str() , int()
Loops
Global variable creation
use of reassignment of variables

x = 1234

x = str(1234)  # type change
multi = 1 # global value creation
for digit in x: #loops
    multi = multi * int(digit)  # interpolation  + reassignment

print multi

It was just an idea. The summation of digits will be solved same way , you have to change just one thing from above code! :slight_smile:


#16

But if i totally can't understand this lesson? Im stop at my exploring Python.


#17

then getting the code will help you further, but its short term help given your understanding doesn't improve so you get confused even more later on

Please post an updated version of your full code so we can help you


#18

I think that's why people drops coding. Sometimes You need just to skip this, instead of just stand on place without any progress. Forever.


#19

Okay, i wrote whole code from zero, using methods, You all gave me.

But still now working.

def digit_sum(n):
    n_str = str(n)
    empty_int = 0
    for digits in n_str:
        empty_int = int(empty_int) + int(digits)
        new_int = str(empty_int)
        return empty_int

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


#20

Almost there!!!! :thumbsup:
Just one thing > Indentation!!
hint**
return empty_int


#21

Oh my godness, it worked.
Thanks You all.


#22

cheers!!!..you did it! :thumbsup:


#23

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