Explain why my code wouldn’t be a solution to this exercise?


#1

Can someone help me out and explain why my code wouldn’t be a solution to this exercise? See below. I am pretty sure the issue is with the calculations within the for loop. I’m also not positive if I converted the number as a string and integer correctly to get the sum. Any help is really appreciated. Thanks.

def digit_sum(n):
  total = 0
  numstring = str(n)
  for digits in numstring:
    total += int(digits)
    return total

How can I break down these problems into easier parts?
#2

by default, return is the last thing a function does. So when a return keyword is reached, the function ends, so your function end in the first iteration of the loop, that can’t be right


#3

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