Need help figuring out this Python Program: Write a program to do the following:
ID: 3688280 • Letter: N
Question
Need help figuring out this Python Program:
Write a program to do the following:
(a) Generate 30 random integers in the range of 1 through 4 and store them in a list. Display the list.
(b) Calculate and display the average of the first 15 elements of the list.
(c) Calculate and display the average of the last 15 elements of the list.
Samle output:
List:
[3,3,3,4,1,4,3,4,1,4,3,2,3,3,2,3,2,1,2,4,2,2,2,4,1,3,4,3,3,3]
Average of the first 15 elements: 2.8666666666666667
Average of the last 15 elements. 2.6
Explanation / Answer
import random
random_list = []
for i in range(30) :
random_list.append(random.randint(1, 4))
j = 0
sum1 = 0
sum2 = 0
for i in random_list:
if j >= 15 :
sum2 = sum2 + i
else :
sum1 = sum1 + i
j = j+1
print "List: ",random_list
print "Average of the first 15 elements: ",sum1 / 15.0
print "Average of the last 15 elements: ",sum2 / 15.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.