Write a Python program: * Program will use a text file data_xxxxxxx.txt to write
ID: 3863502 • Letter: W
Question
Write a Python program:
* Program will use a text file data_xxxxxxx.txt to write 15 numbers in the range of 0 to 100 (one number in each line), where xxxxxxx represents your ID number.
* Then the program will read these values into a list named numbers.
* Call a function get_average( ) and pass the list to it. The function will calculate and return the average back.
* Call a function sort_list( ) to sort scores in the list in ascending order - use any popular algorithm such as bubble sort, insertion sort or merge sort.
* Call a function get_max( ) and pass the list to it. The function will find the highest score and return it back.
* Call a function get_min( ) and pass the list to it. The function will find the lowest score and return it back.
(Your program will not use any pre-written Python functions to find average, max and min. You will write logic for the same)
* Append average, maximum and minimum scores in separate lines into the text file data_xxxxxxx.txt with appropriate messages such as "average score is 90"
Explanation / Answer
code:
from random import randint
def get_average(li):
summ=0
for l in li:
summ+=l
return float(summ)/float(len(li))
def sort_list(li):
li=li.sort()
def get_max(li):
maxx = -1000
for l in li:
if maxx<l:
maxx=l
return maxx
def get_min(li):
minn = 1000
for l in li:
if minn>l:
minn=l
return minn
fw=open("data_xxxxxxx.txt","w")
li =[]
for i in range(1,15):
no = randint(0,100)
li.append(no)
fw.write(str(no)+' ')
fw.write("average score is: "+ str(get_average(li))+' ')
fw.write("minimum is: "+ str(get_min(li))+' ')
fw.write("maximum is: "+ str(get_max(li))+' ')
fw.close()
output of file:
31
4
96
76
61
59
6
59
71
93
37
37
45
37
average score is: 50.8571428571
minimum is: 4
maximum is: 96
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.