Advertisement
Guest User

graphic ages and calculate min max mean

a guest
May 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import matplotlib.pyplot as plot
  2. from random import randint
  3.  
  4. #count amount of people with certain ages
  5. #1 for >1 and <19
  6. #2 for >18 and <41
  7. #3 for >40 and <61
  8. #4 for > 60
  9. def count_function(lista,item):
  10. item_num=0
  11. if item == 0:
  12. for list_item in lista: #search ages between 1 and 18 years and return amount of people
  13. if list_item > 0 and list_item < 19:
  14. item_num += 1
  15. return item_num
  16. elif item == 1:
  17. for list_item in lista: #search ages between 19 and 40 years and return amount of people
  18. if list_item > 18 and list_item < 41:
  19. item_num += 1
  20. return item_num
  21. elif item == 2:
  22. for list_item in lista: #search ages between 41 and 60 years and return amount of people
  23. if list_item > 40 and list_item < 61:
  24. item_num += 1
  25. return item_num
  26. elif item == 3:
  27. for list_item in lista: #search ages older than 60 years and return amount of people
  28. if list_item > 60:
  29. item_num += 1
  30. return item_num
  31. else:
  32. print("Error")
  33.  
  34. #create a list of random ages for 30 people
  35. def create_ages_list():
  36. ages_list = []
  37. for x in range (0, 30):
  38. ages_list.append(randint(1, 80)) # Generate a random number from 0 to 80 and append to list of ages
  39. #print()
  40. return ages_list
  41.  
  42. #create the distribution list of ages acording to the list
  43. #>1 and <19
  44. #>18 and <41
  45. #>40 and <61
  46. #> 60
  47. def create_age_dist_list(ages_dist_list):
  48. count=0
  49. list_p=[]
  50. for item in range(0,4):
  51. list_p.append(count_function(ages_dist_list,count))
  52. count += 1
  53.  
  54. return list_p
  55.  
  56. def graphic_ages(range_ages,ages_dist_list):
  57. plot.bar(range_ages, ages_dist_list, align='center', alpha=0.5)
  58.  
  59. plot.xticks(range_ages)
  60. plot.ylabel('Age range frequency')
  61. plot.xlabel('Age range')
  62. plot.title('Age Ranges')
  63.  
  64. plot.show()
  65. #plot.savefig(fname="Quiz Chart.png")
  66.  
  67. def calculate_max_age(ages_list):
  68. max_age=0
  69. for item in ages_list:
  70. if item > max_age:
  71. max_age=item
  72. return max_age
  73.  
  74. def calculate_min_age(ages_list):
  75. min_age=100
  76. for item in ages_list:
  77. if item < min_age:
  78. min_age=item
  79. return min_age
  80.  
  81. def calculate_mean_age(ages_list):
  82. mean_age=0
  83. for item in ages_list:
  84. mean_age= mean_age + item
  85. mean_age = mean_age / 30
  86. return mean_age
  87.  
  88. #---------------------
  89. #Main - Start
  90. ages_list = []
  91. ages_list=create_ages_list()
  92.  
  93. print (ages_list)
  94.  
  95. ages_dist_l = []
  96. ages_dist_l = create_age_dist_list(ages_list)
  97.  
  98. print(ages_dist_l)
  99.  
  100. max_age = calculate_max_age(ages_list)
  101.  
  102. print("The max age of the list is: " + str(max_age))
  103.  
  104. min_age = calculate_min_age(ages_list)
  105.  
  106. print("The min age of the list is: " + str(min_age))
  107.  
  108. mean_age = int(calculate_mean_age(ages_list))
  109.  
  110. print("The mean age of the list is: " + str(mean_age))
  111.  
  112. graphic_ages(["1 to 18", "19 to 40", "41 to 60", "60 and up"],ages_dist_l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement