15/18 Using "Method 1"


We're told that we can use either "method" to solve the problem. However, I can only get it to work when I use "len", which is seemingly the opposite issue everyone else has (they can ONLY use "Method 1"). Thoughts?

n = [3, 5, 7]

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


The method you are using is based on value at index.

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

The alternate method uses values directly from the list:

def total(x):
    for value in x:
        result += value
    return result

n = [3, 5, 7, 9]
print total(n)        # 24


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