-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_5
More file actions
184 lines (145 loc) · 5.93 KB
/
assignment_5
File metadata and controls
184 lines (145 loc) · 5.93 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
'''
Thea Labuntog thl1733@rit.edu
ISCH-620 Assignment 5: Dictionaries and Recursion
GenAI Statement: I used GenAI to help with code structure and examples of how to use recursion with dictionaries. All code and comments are my own.
'''
# --- helper functions ---
def get_menu_choice():
choice = input("Choose an option (1, 2, 3, or Q/q to quit): ").strip()
return choice
def get_country():
country = input("Enter a country name (or 'Q/q' to quit): ").strip().title()
if country.lower() == 'q':
return None
return country
def get_capital():
capital = input("Enter the capital of the country: ").strip().title()
return capital
def get_continent():
continent = input("Enter the continent of the country: ").strip().title()
return continent
def get_population():
while True:
population = input("Enter the population of the country (in millions, e.g., 331 for 331 million): ").strip()
if population.isdigit():
# round to nearest 100 million
return round(int(population) / 100) * 100
print("Oops! That won't work. Please enter a numeric value for population.")
# --- main program setup ---
def main():
print("Welcome to the Country Info Program!")
print("This program stores countries, their capitals, continents, and populations.\n")
print("This program requires at least three countries to be entered before getting to the fun stuff: viewing and comparing country data.\n")
capitals = {}
continents = {}
populations = {}
print("Please enter information for at least 3 countries.")
count = 0
while count < 3:
country = get_country()
if country is None:
print("You must enter at least three countries!")
continue
if country in capitals:
print("Silly goose detected! This country is already entered. Please enter a different country.")
continue
capital = get_capital()
continent = get_continent()
population = get_population()
capitals[country] = capital
continents[country] = continent
populations[country] = population
count += 1
print("\nYou have entered the following countries:")
print(list(capitals.keys()))
main_menu(capitals, continents, populations)
# --- updating and viewing data ---
def update_countries(capitals, continents, populations):
print("\n--- Update Countries ---")
print("1. Add a country")
print("2. Remove a country")
print("Q/q. Return to main menu")
choice = get_menu_choice()
if choice == '1':
country = get_country()
if country is None:
return
if country in capitals:
print("That country already exists! Please enter a different country.")
return
capital = get_capital()
continent = get_continent()
population = get_population()
capitals[country] = capital
continents[country] = continent
populations[country] = population
print(f"{country} has been added!")
elif choice == '2':
country = get_country()
if country is None:
return
if country not in capitals:
print("Country not found? Please enter a valid country to remove.")
return
del capitals[country]
del continents[country]
del populations[country]
print(f"{country} has been booted!")
elif choice.lower() == 'q':
return
else:
print("Oops! That won't work. Please select 1, 2, or Q/q to quit.")
print("\nCurrent countries:") # display updated list of countries
print(list(capitals.keys()))
def show_country_details(capitals, continents, populations):
country = input("\nEnter a country name to see its details (or 'Q/q' to quit): ").strip().title()
if country.lower() == 'q':
return
if country in capitals:
print(f"\nCountry: {country}")
print(f"Capital: {capitals[country]}")
print(f"Continent: {continents[country]}")
print(f"Population: {populations[country]} million")
else:
print("Country not found!")
return
try:
value = float(input("\nEnter a population value (in millions) to compare: ").strip())
value = round(value / 100) * 100
print(f"\nCountries with population above {value} million:")
for c in populations: # iterate through all countries
if populations[c] > value: # display countries above the value
print(f"- {c} ({populations[c]} million)")
print(f"\nCountries with population below {value} million:")
for c in populations:
if populations[c] < value: # display countries below the value
print(f"- {c} ({populations[c]} million)")
except ValueError:
print("Oops! That won't work. Skipping population comparison.")
# --- recursive menu ---
def main_menu(capitals, continents, populations):
print("\n--- Main Menu ---")
print("1. Add or Remove a Country")
print("2. View Country Details and Population Comparison")
print("3. View All Countries")
print("Q/q. Quit")
choice = input("Choose an option (1, 2, 3, or Q/q to quit): ").strip().lower()
if choice == '1':
update_countries(capitals, continents, populations)
main_menu(capitals, continents, populations)
elif choice == '2':
show_country_details(capitals, continents, populations)
main_menu(capitals, continents, populations)
elif choice == '3':
print("\nCurrent countries:")
print(list(capitals.keys()))
main_menu(capitals, continents, populations)
elif choice == 'q':
print("Exiting the country info program. Goodbye!")
return
else:
print("Oops! That won't work. Please select 1, 2, 3, or Q/q to quit.")
main_menu(capitals, continents, populations)
# --- program entry point ---
if __name__ == "__main__":
main()