In the first section of code, it returns 3 instead of the sum of the digits. The second section of code returns the correct sum.
Why? What's the difference?
I guess if someone could answer the following, it would help explain what's going on to me:
What's the difference between 'for i in range()' and 'for item in list'? From what I understand, the first one loops by index whereas the other does 'for item' does not? Something about this jogs my memory from some previous lessons (don't remember number exactly) but it said that using the 'for x in list' method cannot modify every element in a list, whereas using the 'for i in range()' can because it loops by index? Does that sound familiar?
Could someone explain these two concepts to me?
def digit_sum(n): total = 0 n = str(n) for i in range(0,len(n)): total = total + int(i) return total ----------------------- def digit_sum(n): total = 0 n = str(n) for i in range(0,len(n)): total = total + int(n[i]) return total