forked from werhereitacademy/Python_Modul_Week_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.py
More file actions
109 lines (93 loc) · 3.69 KB
/
Question2.py
File metadata and controls
109 lines (93 loc) · 3.69 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
#1)Create a movie
def create_movie():
name = input("Enter movie name: ")
director = input("Enter director: ")
year = input("Enter release year: ")
genre = input("Enter genre: ")
movie = { #create a dictionary
"name": name, #"name" is the key, and the movie name is its value
"director": director,
"year": year,
"genre": genre
}
return movie
#2)Edit or delete a movie
def edit_movie(movie):
print("Editing movie:", movie["name"])
fields = {"1":"name", "2":"director", "3": "year", "4": "genre"} #fields is a dictionary that maps user choices (like "1") to the keys in the movie dictionary (like "name")
print("1. Name\n2. Director\n3. Year\n4. Genre") #print("1. Name")
#print("2. Director")
#print("3. Year")
#print("4. Genre")
choice = input("What do you want to edit? (1-4): ")
if choice in fields:
new_value = input(f"Enter new {fields[choice]}: ")
movie[fields[choice]] = new_value
else:
print("Invalid choice.")
return movie
def delete_movie(collection, name):
for movie in collection: #goes through the list of movies
if movie ["name"].lower() == name.lower(): #if the name matches, it removes the movie from the list
collection.remove(movie) #use .lower() to make sure it matches even if the user uses different capital letters
print("Movie deleted.")
return
print("Movie not found")
#3)Allow the user to view their collection. List all movies or filter by criteria such as genre or year of release.
def view_movies(collection):
print("1. View all\n2. Filter by genre\n3. Filter by year" )
choice = input("Choose an option: ")
if choice == '1':
for movie in collection:
print(movie)
elif choice == '2':
genre = input("Enter genre: ")
for movie in collection:
if movie["genre"].lower() == genre.lower():
print(movie)
elif choice == '3':
year = input("Enter year: ")
for movie in collection:
if movie["year"] == year:
print(movie)
else:
print("Invalid option.")
#4-Save the movie data in a file and restore this data when you start the program.
import json
def save_to_file(collection, filename="movies.json"):
with open(filename, "w") as f:
json.dump(collection, f)
def load_from_file(filename="movies.json"):
try:
with open(filename, "r") as f:
return json.load(f)
except FileNotFoundError:
return []
def main():
movies = load_from_file()
while True:
print("\n1. Add movie\n2. Edit movie\n3. Delete movie\n4. View collection\n5. Save and Exit")
choice = input("Enter your choice: ")
if choice == '1':
movie = create_movie()
movies.append(movie)
elif choice == '2':
name = input("Enter the movie name to edit: ")
for m in movies:
if m["name"].lower() == name.lower():
edit_movie(m)
break
else:
print("Movie not found.")
elif choice == '3':
name = input("Enter the movie name to delete: ")
delete_movie(movies, name)
elif choice == '4':
view_movies(movies)
elif choice == '5':
save_to_file(movies)
print("Collection saved. Goodbye!")
break
else:
print("Invalid choice. Try again.")
main()