Digit_sum


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


while am using the following input as 'n' in the function (1,12,123,1234,12345,123457..etc) the result obtained is as expected however if I change the sequence (if I used 434, 567, ) I got a different answer.
digit_sum(1234)
[1, 2, 3, 4]
10
correct BUT
digit_sum(434)
[1, 2, 3]
6 INSTEAD of 11.


def digit_sum(n):
	digit_list = []
	word_list = list(str(n))
	counter = len(word_list)
	for string_number in range(1,counter + 1):
		digit_list.append(string_number)
	print(digit_list) # Use to check content of digit_list
	add_digit = sum(digit_list) # Calculating sum
	print(add_digit)


#2

here:

for string_number in range(1,counter + 1):

using range() will not give you actually values of the list, if done right, it will give you a list containing indexes of word_list.


#3

Hmmmm!! I was suspecting that it's the indexes that am adding up.


#4

I have changed the range(1,counter + 1) to word_list and append string_number as int.
Code is working fine. Thank you!


#5