20.6k views
9 votes
8.6 Code Practice: Question 2

Instructions

Copy and paste your code from the previous code practice. If you did not successfully complete it yet, please do that first before completing this code practice.


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

User Jawap
by
6.7k points

1 Answer

7 votes

Answer:

import random

def buildArray(a, n):

for i in range (n):

a.append(random.randint(10,99))

arr = []

def sumArray(a):

tot = 0

for i in range(len(a)):

tot = tot + a [i]

return tot

arr = []

numbers = int(input("How many values to add to the array:\\"))

buildArray(arr, numbers)

print(arr)

print("Total " + str(sumArray(arr)) )

Step-by-step explanation:

User Molly
by
6.0k points