-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcod.txt
More file actions
370 lines (310 loc) · 21.1 KB
/
Copy pathcod.txt
File metadata and controls
370 lines (310 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#Nimna Wijedasa;Mehvish Shakeel
#uses pandas to import files
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
class Country:
def __init__(self,user_country_choice,country_basic_data_array,population_year_data_array,threatened_species_data_array,country_list):
self.choice = user_country_choice
self.country = country_basic_data_array
self.population = population_year_data_array
self.threatened = threatened_species_data_array
self.country_list = country_list
def endangered_species(self,user_threatened_species_choice):
for i in range(len(self.country_list)):
if self.choice == self.threatened[i][0]:
if user_threatened_species_choice == 'p':
print("There are", self.threatened[i][1],"endangered Plants in",self.choice,"." )
elif user_threatened_species_choice == 'f':
print("There are", self.threatened[i][2],"endangered Fish in",self.choice,"." )
elif user_threatened_species_choice == 'b':
print("There are", self.threatened[i][3],"endangered Birds in",self.choice,"." )
else:
print("There are", self.threatened[i][4],"endangered Mammals in",self.choice,"." )
def gen_info(self,un_reigeon_endangered):
un_countries_list = []
plants = []
fish = []
birds = []
mammals = []
for i in range(len(self.country_list)):
if un_reigeon_endangered == self.country[i][1]:
un_countries_list.append(i)
for i in range(len(un_countries_list)):
plants.append(self.threatened[un_countries_list[i]][1])
fish.append(self.threatened[un_countries_list[i]][2])
birds.append(self.threatened[un_countries_list[i]][3])
mammals.append(self.threatened[un_countries_list[i]][4])
endangered_by_un_reigeon_list = [sum(plants),sum(fish),sum(birds),sum(mammals)]
return endangered_by_un_reigeon_list
def pop_den_calc(self,pop_year):
index_year = (pop_year - 1999)
for i in range(len(self.country_list)):
if self.choice == self.population[i][0]:
return (int(self.population[i][index_year])//int(self.country[i][3]))
def pop_change_info(self,starting_year,ending_year):
begin_year = (starting_year - 1999)
end_year = (ending_year-1999)
for i in range(len(self.country_list)):
if self.choice == self.population[i][0]:
print(int(self.population[i][end_year])-int(self.population[i][begin_year]))
def min_max_avg_cal(self,user_pop_info_choice):
if user_pop_info_choice == 'mn':
for i in range(len(self.country_list)):
if self.choice == self.population[i][0]:
minimum_val = min(population_year_data_array[i,1:])
print('the minimum population was ',minimum_val)
elif user_pop_info_choice == 'mx':
for i in range(len(self.country_list)):
if self.choice == self.population[i][0]:
maximum_val = max(population_year_data_array[i,1:])
print('the maximum population was ',maximum_val)
else:
for i in range(len(self.country_list)):
if self.choice == self.population[i][0]:
avg_val = int(np.average(population_year_data_array[i,1:]))
print('the average population was ',avg_val)
def get_graph_val(population_year_data_array,user_country_choice,country_list):
for i in range(len(country_list)):
if user_country_choice == population_year_data_array[i][0]:
return population_year_data_array[i,1:]
def print_invalid():
print('your input is invalid please select a valid option')
def UN_country_data(country_basic_data_array,country_list,user_UN_reigeon_choice,user_country_choice):
for i in range(len(country_list)):
if user_country_choice == country_basic_data_array[i][0]:
if user_UN_reigeon_choice == 'u':
return country_basic_data_array[i][1]
elif user_UN_reigeon_choice == 's':
return country_basic_data_array[i][2]
elif user_UN_reigeon_choice == 'a':
return int(country_basic_data_array[i][3])
country_basic_data = pd.read_csv(r'Country_Data.csv') #We use panda module to transfer the csv file data to the python file.
population_year_data = pd.read_csv(r'Population_Data.csv')
threatened_species_data = pd.read_csv(r'Threatened_Species.csv',sep='\t')
country_basic_data_array = country_basic_data.to_numpy() #We use numpy to create an array.
population_year_data_array = population_year_data.to_numpy()
threatened_species_data_array = threatened_species_data.to_numpy()
country_list=(country_basic_data_array[:,0]) #We slice the array.
users_input_is_valid = False #We create a while loop to ensure a correct country is entered.
while users_input_is_valid == False:
user_country_choice = input("What country would you like to know about?\n") #We prompt the user to enter country.
country_class = Country(user_country_choice,country_basic_data_array,population_year_data_array,threatened_species_data_array,country_list)
if user_country_choice in country_list:
user_secondary_choice_is_valid = False # We assign false to second boolian variable.
while user_secondary_choice_is_valid == False: # We start a while loop for the first menu.
print("\nWhat would you like to know about", user_country_choice ,"?\n ",
"\n Endangerd species - e\n",
" UN Country data - c\n",
" Population - p\n",
" General inormation about another country (graph) - g\n",
" Compare population trends across two countries (graph) - pl\n"
" Return to previous menu - q\n")
user_country_info_choice = input('Please select the appropirate letter corresponding to your choice (case sensitive) : ')
if user_country_info_choice == 'q': # We end the loop
user_secondary_choice_is_valid = True # We end the loop by asigning false to the second boolean variable
elif user_country_info_choice == 'g':
third_boolean = False
while third_boolean == False: # We create another loop by asigning false to the thrid boolean variable.
print('What kind of General inormation about another country would you like to know about?\n', # For second menu with prompt.
'Endangered species by UN region - r\n',
'A comparison of endangered species by UN region (graph) - c\n'
" Return to previous menu - q\n")
user_UN_endangered_choice = input('Please select the appropirate letter corresponding to your choice (case sensitive) : ')
if user_UN_endangered_choice == 'q':
third_boolean = True
break #we end the current loop
elif user_UN_endangered_choice == 'r':
third_boolean = True
fourth_boolean = False #We create another loop by asigning the forth boolean as false.
while fourth_boolean == False:
print("Which UN regeon would you like to know about?\n", #For third menu with prompt.
"Asia\n",
"Europe\n",
"Africa\n",
"Americas\n",
"Oceania\n",
"To return to the previous menu type - 'q'\n")
un_reigeon_endangered = input('Please select the appropirate region corresponding to your choice (case sensitive) : ')
if un_reigeon_endangered == 'Asia':
fourth_boolean = True
user_secondary_choice_is_valid = True
users_input_is_valid = True
threat_list = country_class.gen_info(un_reigeon_endangered) #We use object instance of the class.
print('In',un_reigeon_endangered,'there are',threat_list[0], 'plants',threat_list[1],'fish',threat_list[2],'birds',threat_list[3],'mammals')
elif un_reigeon_endangered == 'Europe':
fourth_boolean = True
users_input_is_valid = True
user_secondary_choice_is_valid = True
threat_list = country_class.gen_info(un_reigeon_endangered)
print('In',un_reigeon_endangered,'there are',threat_list[0], 'plants',threat_list[1],'fish',threat_list[2],'birds',threat_list[3],'mammals')
elif un_reigeon_endangered == 'Africa':
fourth_boolean = True
users_input_is_valid = True
User_secondary_choice_is_valid = True
threat_list = country_class.gen_info(un_reigeon_endangered)
print('In',un_reigeon_endangered,'there are',threat_list[0], 'plants',threat_list[1],'fish',threat_list[2],'birds',threat_list[3],'mammals')
elif un_reigeon_endangered == 'Americas':
fourth_boolean = True
users_input_is_valid = True
User_secondary_choice_is_valid = True
threat_list = country_class.gen_info(un_reigeon_endangered)
print('In',un_reigeon_endangered,'there are',threat_list[0], 'plants',threat_list[1],'fish',threat_list[2],'birds',threat_list[3],'mammals')
elif un_reigeon_endangered == 'Oceania':
fourth_boolean = True
users_input_is_valid = True
user_secondary_choice_is_valid = True
threat_list = country_class.gen_info(un_reigeon_endangered)
print('In',un_reigeon_endangered,'there are',threat_list[0], 'plants',threat_list[1],'fish',threat_list[2],'birds',threat_list[3],'mammals')
elif un_reigeon_endangered == 'q':
fourth_boolean = True
else:
print_invalid()
elif user_UN_endangered_choice == 'c':
users_input_is_valid = True
endangered_species_sum = [sum(country_class.gen_info('Asia')),sum(country_class.gen_info('Europe')),sum(country_class.gen_info('Africa')),sum(country_class.gen_info('Americas')),sum(country_class.gen_info('Oceania'))]
reigeon = ['Asia', 'Europe', 'Africa', 'Americas', 'Oceania']
fig = plt.figure(figsize = (10, 5))
plt.bar(reigeon, endangered_species_sum, color=['orange', 'purple', 'red', 'blue', 'cyan'],width = 0.4)
plt.ylabel('number of endangered species')
plt.xlabel('UN reigeon')
plt.title("Endangered species by UN reigeon")
plt.show()
third_boolean = True
user_secondary_choice_is_valid = True
else:
print_invalid()
elif user_country_info_choice == 'p':
user_secondary_choice_is_valid = True
users_input_is_valid = True
secondary_boolean = False
while secondary_boolean == False:
print(f"what would you like to know about{user_country_choice}'s population\n",
'change over time - ct\n',
'maximum population - mn\n',
'minimum populatio - mx\n',
'average population across all years - ap\n',
'return to previos menu - q\n')
user_pop_info_choice = input('your choice (letter is case sensitive)\n')
if user_pop_info_choice == 'q':
secondary_boolean = True
user_secondary_choice_is_valid = False
elif user_pop_info_choice == 'ap':
secondary_boolean = True
country_class.min_max_avg_cal(user_pop_info_choice)
elif user_pop_info_choice == 'mn':
secondary_boolean = True
country_class.min_max_avg_cal(user_pop_info_choice)
elif user_pop_info_choice == 'mx':
secondary_boolean = True
country_class.min_max_avg_cal(user_pop_info_choice)
elif user_pop_info_choice == 'ct':
secondary_boolean = True
fourth_boolean = False
while fourth_boolean == False:
print( 'please enter a starting year and ending year\n')
starting_year = int(input('starting year'))
ending_year = int(input('ending year'))
if (starting_year not in range(1999,2021)) or (ending_year not in range(1999,2021)):
print('please enter a starting and ending year in the range 2000 - 2020 (inclusive)\n')
elif starting_year > ending_year:
print("the starting year can't be grater than the ending year" )
else:
fourth_boolean = True
country_class.pop_change_info(starting_year,ending_year)
else:
print_invalid()
elif user_country_info_choice == 'c':
user_secondary_choice_is_valid = True
users_input_is_valid = True
third_boolean = False
while third_boolean == False:
print(f'\nWhat would you like to know about {user_country_choice}\'s UN data?\n please select the appropriate letter' ,
"\n UN reigeon - u\n",
" UN sub-reigeon -s\n",
" area - a\n",
" population density - p\n",
" Return to previous menu - q\n")
user_UN_reigeon_choice = input('your choice (letter is case sensitive)\n')
if user_UN_reigeon_choice == 'q':
third_boolean = True
user_secondary_choice_is_valid = False
elif user_UN_reigeon_choice == 'u' :
third_boolean = True
print(UN_country_data(country_basic_data_array,country_list,user_UN_reigeon_choice,user_country_choice))
elif user_UN_reigeon_choice == 's':
third_boolean = True
print(UN_country_data(country_basic_data_array,country_list,user_UN_reigeon_choice,user_country_choice))
elif user_UN_reigeon_choice =='a':
third_boolean = True
print(UN_country_data(country_basic_data_array,country_list,user_UN_reigeon_choice,user_country_choice))
elif user_UN_reigeon_choice == 'p':
fourth_boolean = False
while fourth_boolean == False:
pop_year = int(input('please enter a year you want the population density for\n'))
if (pop_year not in range(1999,2021)):
print('please enter a year in the range 2000 - 2020 (inclusive)\n')
else:
fourth_boolean = True
third_boolean = True
print("the population density in",pop_year,"is",country_class.pop_den_calc(pop_year),'people per square Kilometer')
else:
print_invalid()
elif user_country_info_choice == 'e':
user_secondary_choice_is_valid = True
users_input_is_valid = True
third_boolean = False
while third_boolean == False:
print(f"\nWhich endangered species of {user_country_choice} would you like to know about?\n",
"\n Plants - p\n",
" Fish - f\n",
" Birds - b\n",
" Mammals - m\n",
" Return to previous menu - q\n")
user_threatened_species_choice = input('Please select the appropirate letter corresponding to your choice (case sensitive) : ')
if user_threatened_species_choice == 'q':
third_boolean = True
user_secondary_choice_is_valid = False
elif user_threatened_species_choice == 'p' or 'f' or 'b' or 'm':
country_class.endangered_species(user_threatened_species_choice)
third_boolean = True
else:
print_invalid()
elif user_country_info_choice == 'pl':
y = get_graph_val(population_year_data_array,user_country_choice,country_list)
x=[]
for i in range(2000,2021):
x.append(i)
y_label = user_country_choice
plt.suptitle('Comparison of population across two countries')
plt.subplot(2,1,1)
plt.plot(x,y, label = user_country_choice)
plt.ylabel('population')
plt.xlabel('Year')
plt.legend()
plt.xticks(x,x)
third_boolean = False
while third_boolean == False:
user_compare_plot_choice_country = input('What other country would you like to compare\n')
if user_compare_plot_choice_country in country_list:
y = get_graph_val(population_year_data_array,user_compare_plot_choice_country,country_list)
x=[]
for i in range(2000,2021):
x.append(i)
y2_label = user_compare_plot_choice_country
plt.subplot(2,1,2)
plt.plot(x,y,'r', label = user_compare_plot_choice_country)
plt.ylabel("population")
plt.xlabel('Year')
plt.xticks(x,x)
plt.legend()
plt.show()
third_boolean = True
else:
print('please enter a valid country')
user_secondary_choice_is_valid = True
users_input_is_valid = True
else:
print_invalid()
else:
print_invalid()