Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help figuring out this Python Program: Write a program to do the following:

ID: 3688601 • 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

I am useing python 3

#import random here

import random

# this function define to get randoms number

def getrandomsNumber():

list_randoms=[]

# how many time run this loop. you change if you want more then 30 or less then

for i in range (30):

# create random value between 1 to 4

# random.randrange(1,5) use 1 low value and 5 high high value +1

list_randoms.append(random.randrange(1,5))

# return final list here

return list_randoms

  

# call randoms function here

randoms_list = getrandomsNumber()

# this function use for Calculate average

def getAvg(v):

total = float(sum(v))

count = float(len(v))

if v == []:

return False

return float(total/count)

  

print("List: ")

print (randoms_list)

# Average of the first 15 elements

# randoms_list[:15] use for get first 15 value in list

avg_first_val = randoms_list[:15]

avg_first = getAvg(avg_first_val)

print("Average of the first 15 elements: "+str(avg_first))

# Average of the last 15 elements

# randoms_list[-15:] use for get last 15 value in list

avg_last_val = randoms_list[-15:]

avg_last = getAvg(avg_last_val)

print("Average of the last 15 elements: "+str(avg_last))

Output of this code:-

List:

[4,1,4,2,4,1,2,3,4,2,4,1,3,3,3,2,1,1,3,2,3,4,2,3,2,2,1,2,1,4]

Average of the first 15 elements: 2.7333333333333334

Average of the last 15 elements: 2.2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote