How can I break down these problems into easier parts?

on the right hand side of the assign operator, there you can cast values to a specific type, which are then stored in the variable, simple example:

variable = int(value)

Thanks. So now I have:

def digit_sum(n):
  number = 0
  for number in str(n):
    number = int(number) + int(number)
    return number
  
print digit_sum(434)

But when running the code it returns 8 when it should return 11.

The iterator variable should not be the same name as the accumulator variable. Try using a different name.

Ignore this post. Ignore this post.

Okays, so I got it to add the first argument to itself (the following code prints 6 to terminal). How do I get it to add the numbers in the proper sequence?

def digit_sum(n):
  total = 0
  string = str(n)
  for number in string:
    total = int(number) + int(number)
    return total
  
print digit_sum(34)

If we look closely we can see where that comes from…

int(number) + int(number)

which is essentially, 2 * number. The indented return causes the function to exit on the first iteration…

int(3) + int(3) => 6

To correct the first issue,

total = total + int(number)

To correct the second issue, unindent so the return lines up with for.

for ...:
    # code
return total
1 Like

u made it 100 times complex then i thought it can be, thanks for the comparison, altho all those operations u made were the only way? (the long code u made) or is the a simpler yet more advance way ?

Two basic rules of thumb are, simplicity/brevity and correctness/reliability. Any method that meets these two criteria is well on the way to being a viable approach. At this stage of learning we are more interested in arriving at many possible solutions, not just the best one.

ohh i see, and in the message i wrote the = there*
and thanks for your advice

1 Like

In the beginning of your for loop, your “q” has been reset to int(n)%10, which means your q kept being 1 (6061%10) every time. To solve this, you should have q=int(n) before the for loop and change the for loop to
total +=q%10
q//=10
In this way, your q changes every time

In my opinion simplest way to solve this is to use recursion, although that might be out of the scope of what was taught so far :v

def digit_sum(n):
if n < 10:
return n
else:
return n % 10 + digit_sum(n/10)

For some reason that is probably very obvious my code returns 2 values. When it is run for the figures 434 I get the following results:
0
11
I think I should only be seeing 11 which is the correct maths, I’m not sure why it still shows the initial zero.

def digit_sum(n):
  total = 0
  for num in str(n):
    total += int(num)
  print total

I’ve checked the indentation in sublime and it looks good (I think).
image

The exercise does validation, which is done by calling the function, given you use print in the function, this output also displays

To be truly useful, a function will return, and let the caller do the printing or other evaluation of the returned value. Your function above will return None.