forked from werhereitacademy/Python_Modul_Week_2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMohammad Q2.py
More file actions
151 lines (141 loc) · 4.55 KB
/
Mohammad Q2.py
File metadata and controls
151 lines (141 loc) · 4.55 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
# Question 2: Film Library Management System Project
print("Question 2")
import json
# File to store the movie data
MOVIE_FILE = 'movies.json'
# Load movie data from file
def load_movies():
name = input("Enter movie name: ")
director = input("Enter director: ")
year = input("Enter release year: ")
genre = input("Enter genre: ")
movie = {
"name": name,
"director": director,
"release_year": year,
"genre": genre
}
return movie
# Save movie data to file
def save_movies(movies):
with open(MOVIE_FILE, 'w') as f:
json.dump(movies, f, indent=4)
# Add a new movie
def add_movie(movies):
name = input("Enter movie name: ").strip()
if not name:
print("Movie name cannot be empty.")
return
if name in movies:
print("Movie already exists.")
return
director = input("Enter director: ").strip()
year = input("Enter release year: ").strip()
while not year.isdigit():
year = input("Invalid year. Enter a valid release year: ").strip()
genre = input("Enter genre: ").strip()
movies[name] = {
'director': director,
'year': year,
'genre': genre
}
print("Movie added.")
# Edit movie details
def edit_movie(movies):
name = input("Enter movie name to edit: ").strip()
if name not in movies:
print("Movie not found.")
return
print("What do you want to edit?")
print("1. Director")
print("2. Release Year")
print("3. Genre")
choice = input("Choose (1-3): ").strip()
if choice == '1':
movies[name]['director'] = input("Enter new director: ").strip()
elif choice == '2':
year = input("Enter new release year: ").strip()
while not year.isdigit():
year = input("Invalid year. Enter a valid release year: ").strip()
movies[name]['year'] = year
elif choice == '3':
movies[name]['genre'] = input("Enter new genre: ").strip()
else:
print("Invalid choice.")
return
print("Movie updated.")
# Delete a movie
def delete_movie(movies):
name = input("Enter movie name to delete: ").strip()
if name in movies:
del movies[name]
print("Movie deleted.")
else:
print("Movie not found.")
# View the movie collection
def view_movies(movies):
print("1. View all movies")
print("2. Filter by genre")
print("3. Filter by release year")
choice = input("Choose an option: ").strip()
if choice == '1':
if not movies:
print("No movies in the library.")
else:
for name, details in movies.items():
print(f"\nTitle: {name}")
print(f" Director: {details['director']}")
print(f" Year: {details['year']}")
print(f" Genre: {details['genre']}")
elif choice == '2':
genre = input("Enter genre to filter: ").strip().lower()
found = False
for name, details in movies.items():
if details['genre'].lower() == genre:
print(f"\nTitle: {name}")
print(f" Director: {details['director']}")
print(f" Year: {details['year']}")
print(f" Genre: {details['genre']}")
found = True
if not found:
print("No movies found for that genre.")
elif choice == '3':
year = input("Enter release year to filter: ").strip()
found = False
for name, details in movies.items():
if details['year'] == year:
print(f"\nTitle: {name}")
print(f" Director: {details['director']}")
print(f" Year: {details['year']}")
print(f" Genre: {details['genre']}")
found = True
if not found:
print("No movies found for that year.")
else:
print("Invalid choice.")
# Main menu
def main():
movies = load_movies()
while True:
print("\n--- Movie Library Menu ---")
print("1. Add Movie")
print("2. Edit Movie")
print("3. Delete Movie")
print("4. View Movies")
print("5. Exit")
choice = input("Choose an option: ").strip()
if choice == '1':
add_movie(load_movies)
elif choice == '2':
edit_movie(movies)
elif choice == '3':
delete_movie(movies)
elif choice == '4':
view_movies(movies)
elif choice == '5':
save_movies(movies)
print("Goodbye!")
break
else:
print("Invalid option.")
main()