Digit_sum

#1

please help i dont understand i revised the past things i have taken but need help on this exercise

#2

Please show us what you have so far. What part of the instructions do you now understand?

Given a number with two or more digits, what is the sum of the digits?

Eg.

``````number = 23456

sum_of_digits = 2 + 3 + 4 + 5 + 6
5
9
14
20``````

Of course, the order in which we add the digits is not important. All that matters is that we add all of them. Can you think of an algorithm that can break a number into digits and sum them up?

#3

it is not the instructions i cant some how solve it and what is the floor divide and why do i make it a str when its an integer why cant i just write:
`
def digit_sum(n):
return sum(n)
or
def digit_sum(n):
for numbers in n:
return sum(numbers)
look i revised the past things and i have been very well in the past exercises what is the problem with this

#4

The sum function works on a list of numbers. We convert `n` to a string so we can convert it to a list (a number cannot be split or made into a list since Number class has no method to perform that task).

``return sum(list(str(n)))``

The main idea behind these exercises is to attempt to solve these without using built-in tools. That means some form of algorithm. We're not looking for the best program, but a better understanding of how to create a process that solves the problem.

There is another possibility that does not involve a list, but that simply iterates over the string. To add each digit we need to convert it back to an integer, else we end up concatenating a new string that is identical to the original.

#5

This operator, `//` symbolizes floor division, or integer division. The result is always rounded down to the nearest integer.

``````20 // 17 == 1

20 % 17 == 3

1 * 17 + 3 == 20``````

When we floor divide a number by 10, the quotient will be one less digit than the dividend.

``````total = 0

1. 12345 % 10 == 5  => digit 5 extracted from sequence; add to total

2. 12345 // 10 == 1234  => repeat step 1``````

#6

look i know this sounds like i dont understand and these things but i have two or three days without programming because of important things whatever this is my code and it gives the same error you said that the str can turn into a list so i did that tell me what is the problem here

``````def digit_sum(n):
n == str(n)
kalo = 0
for numbers in n :
sum(numbers)
kalo += sum(numbers)
return kalo``````

and why some people type that
remainder = n % 10
or remainder = n / 10 to get the sum its sum not division i was thinking why

#7

wait a second i think i got it i didnt read what you wrote all

#9