Question

Write a Python Program

Preview File Edit View Go Tools Window OO 95% Help An Intro to Programming Using Python (2) (1) (1).pdf (page 256 of 431) Mon

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

import math

# this function calculates the mean of the list l
def calc_mean(l):
   return sum(l)/len(l)

# this function calculates the stadard deviation of the list l
def calc_stdDev(l):
   m = sum(l)/len(l) # mean
   n = len(l) # number of elements in the list
   temp = 0

   # calculate sum of squared diff. from mean
   for i in l:
       temp+=(i-m)**2

   return math.sqrt(temp/n)  

# this function calculates the grade of the student depending on the exam score, mean and stdDev
def assign_grade(EX,m,s):
   if EX >= (m+1.5*s):
       return 'A'
   elif EX >= (m+0.5*s) and EX < (m+1.5*s):
       return 'B'
   elif EX >= (m-0.5*s) and EX < (m+0.5*s):
       return 'C'
   elif EX >= (m-1.5*s) and EX < (m-0.5*s):
       return 'D'
   elif EX < (m-1.5*s):
       return 'F'

# main function
def main():
   file_object = open('Scores.txt') # open the file
   # the elements in the text file are exam scores of students, one score per line
   list_of_scores = []
   num_of_scores = 0
   for item in file_object.readlines():
       exam_score = int(item.split('\n')[0])
       list_of_scores.append(exam_score)
       num_of_scores+=1

   m = calc_mean(list_of_scores) # mean
   s = calc_stdDev(list_of_scores) # standard deviation  
  
   # print the mean and standard deviation
   print("Number of scores: "+str(num_of_scores))
   print("Average of scores: "+str(m))
   print("Standard Deviation of scores: "+str(s))

   # print out the grade of the students
   for i in range(num_of_scores):
       grade = assign_grade(list_of_scores[i],m,s)
       print("Exam score: "+str(list_of_scores[i])+", Grade:"+grade)

if __name__ == '__main__':
   main()          

Code Snapshot:

import math # this function calculates the mean of the list i def calc_mean(l): return sum(L)/len(1) # this function calculat

# main function def main(): file object = open(Scores.txt) # open the file # the elements in the text file are exam scores

Output:

Number of scores: 14 Average of scores: 68 Standard Deviation of scores: 14.3527000944 Exam score: 59, Grade:D Exam score: 60

Add a comment
Know the answer?
Add Answer to:
Write a Python Program Preview File Edit View Go Tools Window OO 95% Help An Intro...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT