Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plot
- from random import randint
- #count amount of people with certain ages
- #1 for >1 and <19
- #2 for >18 and <41
- #3 for >40 and <61
- #4 for > 60
- def count_function(lista,item):
- item_num=0
- if item == 0:
- for list_item in lista: #search ages between 1 and 18 years and return amount of people
- if list_item > 0 and list_item < 19:
- item_num += 1
- return item_num
- elif item == 1:
- for list_item in lista: #search ages between 19 and 40 years and return amount of people
- if list_item > 18 and list_item < 41:
- item_num += 1
- return item_num
- elif item == 2:
- for list_item in lista: #search ages between 41 and 60 years and return amount of people
- if list_item > 40 and list_item < 61:
- item_num += 1
- return item_num
- elif item == 3:
- for list_item in lista: #search ages older than 60 years and return amount of people
- if list_item > 60:
- item_num += 1
- return item_num
- else:
- print("Error")
- #create a list of random ages for 30 people
- def create_ages_list():
- ages_list = []
- for x in range (0, 30):
- ages_list.append(randint(1, 80)) # Generate a random number from 0 to 80 and append to list of ages
- #print()
- return ages_list
- #create the distribution list of ages acording to the list
- #>1 and <19
- #>18 and <41
- #>40 and <61
- #> 60
- def create_age_dist_list(ages_dist_list):
- count=0
- list_p=[]
- for item in range(0,4):
- list_p.append(count_function(ages_dist_list,count))
- count += 1
- return list_p
- def graphic_ages(range_ages,ages_dist_list):
- plot.bar(range_ages, ages_dist_list, align='center', alpha=0.5)
- plot.xticks(range_ages)
- plot.ylabel('Age range frequency')
- plot.xlabel('Age range')
- plot.title('Age Ranges')
- plot.show()
- #plot.savefig(fname="Quiz Chart.png")
- def calculate_max_age(ages_list):
- max_age=0
- for item in ages_list:
- if item > max_age:
- max_age=item
- return max_age
- def calculate_min_age(ages_list):
- min_age=100
- for item in ages_list:
- if item < min_age:
- min_age=item
- return min_age
- def calculate_mean_age(ages_list):
- mean_age=0
- for item in ages_list:
- mean_age= mean_age + item
- mean_age = mean_age / 30
- return mean_age
- #---------------------
- #Main - Start
- ages_list = []
- ages_list=create_ages_list()
- print (ages_list)
- ages_dist_l = []
- ages_dist_l = create_age_dist_list(ages_list)
- print(ages_dist_l)
- max_age = calculate_max_age(ages_list)
- print("The max age of the list is: " + str(max_age))
- min_age = calculate_min_age(ages_list)
- print("The min age of the list is: " + str(min_age))
- mean_age = int(calculate_mean_age(ages_list))
- print("The mean age of the list is: " + str(mean_age))
- 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