Question #170139

After your program has prompted the user for how many values should be in the array, generated those values, and printed the whole list, create and call a new function named sumArray. In this method, accept the array as the parameter. Inside, you should sum together all values and then return that value back to the original method call. Finally, print that sum of values.

Sample Run

How many values to add to the array: 
8
[17, 99, 54, 88, 55, 47, 11, 97]
Total 468

Expert's answer

import random


def sumArray(ls):
    lst_sum = 0
    for element in ls:
        lst_sum += element
    return lst_sum


print("How many values to add to the array:")
n = int(input())
lst = []
for i in range(n):
    lst.append(random.randint(0, 100))
print(lst)
sum_array = sumArray(lst)
print('Total ', sum_array)
LATEST TUTORIALS
APPROVED BY CLIENTS