Advertisement
GCK

GCK/ count occurences function for all possibles scores

GCK
Sep 26th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1.  
  2. from random import randint
  3. import matplotlib.pyplot as plot
  4.  
  5.  
  6. # generate random marks out of 10 to represent the results of the quiz taken by  a class
  7. marks=[randint(0,10) for x in range(30) ]
  8. # quiz scored out of 10
  9. possible_scores=[x for x in range(11)]
  10.  
  11. def count_occurences(occurence,list_of_stuff):
  12.     count=0
  13.     for item in list_of_stuff:
  14.         if item==occurence:
  15.             count+=1
  16.     print("{} appears {} times in the list".format(occurence,count))
  17.     return count
  18.  
  19.  
  20. def count_occurences_ofeach_tolist(possible_scores):
  21.     list_of_scores=[]
  22.    
  23.     for score in possible_scores:
  24.         count=count_occurences(score,marks)
  25.         list_of_scores.append(count)
  26.            
  27.     return list_of_scores
  28.  
  29. list_of_scores=count_occurences_ofeach_tolist(possible_scores)
  30. print(list_of_scores)
  31.  
  32. plot.barh(range(11), list_of_scores, align='edge', alpha=1.0, color="red",)
  33.  
  34. plot.xticks(range(11))
  35. plot.xlabel('Score frequency')
  36. plot.ylabel('Marks range')
  37. plot.title('Scores of class from random generated marks')
  38.  
  39. plot.show()
  40. plot.savefig(filename="Scores_chart_from_random_marks.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement