Python Lesson 15 Bug


The array n contains the numbers 3, 5 and 7. We are supposed to use a function that loops through the array and adds the items together. They equal 15. However the assignment says they should equal 9 and shows an array in the error message that has 0, 3 and 6.

It should return as completed.

n = [3, 5, 7]

def total(numbers):
    result = 0
    for i in range(len(numbers)):
        result += n[i]
    return result
print total(n)


This apparently works if you if you use the following loop:

for i in numbers:
result += i
print "Number:" + str(i)
print "Results:" + str(result)

However the lesson says you can use both.


The error it prints out is still using an incorrect array.


okay, when you have a function you have a parameter:

def total(numbers):

numbers is your parameter, it will hold the value which is passed as argument:

def total(numbers):
    print numbers


this way, your function works for multiply lists, but then it is important to use the function parameter in this line:

result += n[i]

and not n which is a fixed list.


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