-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12uloha.py
More file actions
92 lines (65 loc) · 2.56 KB
/
Copy path12uloha.py
File metadata and controls
92 lines (65 loc) · 2.56 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
import pickle
class Country():
def __init__(self):
self.country_capitals = {}
def add_country(self,country, capital):
self.country_capitals[country] = capital
print(f"Added: {country} -> {capital}")
def delete_country(self,country):
if country in self.country_capitals:
del self.country_capitals[country]
print(f"Deleted: {country}")
else:
print(f"{country} not found.")
def find_country(self,country):
return self.country_capitals.get(country, "Not found")
def edit_country(self,country, new_capital):
if country in self.country_capitals:
self.country_capitals[country] = new_capital
print(f"Updated: {country} -> {new_capital}")
else:
print(f"{country} not found.")
def show_all(self):
for key,val in self.country_capitals.items():
print(key,val)
def save_data(self,filename="countries.pkl"):
with open(filename, "wb") as file:
pickle.dump(self.country_capitals, file)
print("Data saved successfully.")
def load_data(self,filename="countries.pkl"):
try:
with open(filename, "rb") as file:
self.country_capitals = pickle.load(file)
print("Data loaded successfully.")
except FileNotFoundError:
print("No saved data found.")
if __name__ == "__main__":
cntr = Country()
while True:
print("\nOptions: add, delete, find, edit, show_all, save, load, exit")
action = input("Enter action: ").strip().lower()
if action == "add":
country = input("Enter country name: ")
capital = input("Enter capital: ")
cntr.add_country(country, capital)
elif action == "delete":
country = input("Enter country to delete: ")
cntr.delete_country(country)
elif action == "find":
country = input("Enter country to find: ")
print(f"Capital: {cntr.find_country(country)}")
elif action == "edit":
country = input("Enter country to edit: ")
new_capital = input("Enter new capital: ")
cntr.edit_country(country, new_capital)
elif action == "show_all":
cntr.show_all()
elif action == "save":
cntr.save_data()
elif action == "load":
cntr.load_data()
elif action == "exit":
print("Exiting program.")
break
else:
print("Invalid action, please try again.")