Advertisement
johnpentyrch

meanfunction

May 1st, 2020
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. import matplotlib.pyplot as plot
  2. import random
  3.  
  4. def mean_score(alist):
  5.     sum=0
  6.     counts = len(alist)    # Inumber of scores in list
  7.     for score in alist:
  8.         sum += score
  9.         mean=sum/counts
  10.     return mean
  11.  
  12.  
  13. def count(mark,alist):
  14.     counts = 0    # Initialise a variable for counting scores of ten
  15.     for countr in alist:
  16.       if countr == mark:
  17.         counts += 1
  18.     return counts
  19.  
  20. def ucount(mark,alist):
  21.     counts = 0    # Initialise a variable for counting scores of ten
  22.     for countr in alist:
  23.       if countr < mark:
  24.         counts += 1
  25.     return counts
  26.  
  27. def numberAtEachCore(alist):
  28.     #create an zeroed results 'array'
  29.     res=[]
  30.     for i in range(11):
  31.         res.append(0)
  32.     for cScore in alist:
  33.         res[cScore]+=1
  34.     return res
  35.    
  36. def print_data(alist):
  37.     print('\nStudent Performance')
  38.     for i in range(len(alist)):
  39.         print('{0} students) scored {1}'.format(alist[i],i))
  40.        
  41.  
  42.  
  43.  
  44. #set up some sample scores
  45. scores = []
  46.  
  47. for x in range (0, 50):
  48.   scores.append(random.randint(0, 10))
  49.  
  50. print('Generated \'random\' scores: {0}'.format(scores))
  51.  
  52. top_scorers = count(10, scores)
  53. lowmark=4
  54. low_scorers = ucount(lowmark, scores)
  55. themean = mean_score(scores)
  56. print("{0} learners got top marks".format(top_scorers))
  57. print("{0} learners got under {1} marks".format(low_scorers,lowmark))
  58. print("{0} mean score for these data".format(themean))
  59.  
  60.  
  61. performance=numberAtEachCore(scores)
  62. print(performance)
  63. print_data(performance)
  64.  
  65. #plot results with red bars for fails 3 or less,
  66. #orange 4, yellow satisfactory 5-8,
  67. #green excellent 9 or 100
  68.  
  69. plot.bar(range(11),  performance, align='center',alpha=0.99, color=['red', 'red', 'red', 'red', 'orange', 'yellow', 'yellow', 'yellow', 'yellow', 'green', 'green'])
  70.  
  71. plot.xticks(range(11))
  72. plot.ylabel('Score frequency')
  73. plot.xlabel('Score')
  74. plot.title('Scores on a quiz\nred: poor,orange:OK,yellow:satisfactory,green:excellent')
  75. plot.axhline(y=themean, xmin=0.0, xmax=1.0, color='b')
  76. plot.show()
  77. plot.savefig(fname="QuizChart.png")
  78. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement