I got stuck at this exercise, but managed to solve it at last. My goal was to be able to use both method 1 and method 2. See explanation to both methods below the code.
n = [3, 5, 7] def total(numbers): result = 0 for i in range(len(numbers)): result += numbers[i] return result print total(n)
n = [3, 5, 7] def total(numbers): result = 0 for i in numbers: result += i return result print total(n)
The difference between using the both:
I think it's beacuse range allows for looping through the index numbers, which in this case is up to, but not including 3. Index 0, 1, 2. Thus result += numbers[i] will be the same as result += n[index]. n[index] in turn represents the value at the given index in the n list. Basically, range allows for assigning the index numbers of a list to variable i.
Whilst when using for i in numbers:, the for loop will iterate through the actul values of the list to the variable i. Thereby we only need to write reseult += i.
I thought this could be useful for those who get stuck at this step. I'm new at programming, so please correct me if my assumptions are wrong.