In: Computer Science
Modify the Movie List 2D program
-Modify the program so it contains four columns: name, year, price and rating (G,PG,R…)
-Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating
def list(movie_list):
if len(movie_list) == 0:
print("There are no movies in the list.\n")
return
else:
i = 1
for row in movie_list:
print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")")
i += 1
print()
def add(movie_list):
name = input("Name: ")
year = input("Year: ")
movie = []
movie.append(name)
movie.append(year)
movie_list.append(movie)
print(movie[0] + " was added.\n")
def delete(movie_list):
number = int(input("Number: "))
if number < 1 or number > len(movie_list):
print("Invalid movie number.\n")
else:
movie = movie_list.pop(number-1)
print(movie[0] + " was deleted.\n")
def display_menu():
print("COMMAND MENU")
print("list - List all movies")
print("add - Add a movie")
print("del - Delete a movie")
print("exit - Exit program")
print()
def main():
movie_list = [["Monty Python and the Holy Grail", 1975],
["On the Waterfront", 1954],
["Cat on a Hot Tin Roof", 1958]]
display_menu()
while True:
command = input("Command: ")
if command == "list":
list(movie_list)
elif command == "add":
add(movie_list)
elif command == "del":
delete(movie_list)
elif command == "exit":
break
else:
print("Not a valid command. Please try again.\n")
print("Bye!")
if __name__ == "__main__":
main()
Program:
def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1 print() def add(movie_list): name = input("Name: ") year = input("Year: ") price=int(input("Price: ")) #taking price rating=float(input("Rating (1 - 10):")) #taking rating movie = [] movie.append(name) movie.append(year) movie.append(price) #adding price to movie list movie.append(rating) #adding rating to movie list movie_list.append(movie) print(movie[0] + " was added.\n") def delete(movie_list): number = int(input("Number: ")) if number < 1 or number > len(movie_list): print("Invalid movie number.\n") else: movie = movie_list.pop(number - 1) print(movie[0] + " was deleted.\n") def find_by_rating(movie_list,rating): #find function if len(movie_list)==0: print('Add movies to find') return else: l=[] #list to store the movies for i in movie_list: #loop if i[3]==rating: l.append(i[0]) if len(l)==0: print("No movies are present with given rating") else: print("movies are:") for i in l: #loop print(i) #printing the movies def display_menu(): print("COMMAND MENU") print("list - List all movies") print("add - Add a movie") print("del - Delete a movie") print("find - find by rating") print("exit - Exit program") print() movie_list = [["Monty Python and the Holy Grail", 1975,200,5],["On the Waterfront", 1954,400,6],["Cat on a Hot Tin Roof", 1958,300,8]] display_menu() while True: command = input("Command: ") if command == "list": list(movie_list) elif command == "add": add(movie_list) elif command == "del": delete(movie_list) elif command == "find": #added find command rating=int(input("Enter rating: ")) #taking rating find_by_rating(movie_list,rating) #calling find by rating function elif command == "exit": break else: print("Not a valid command. Please try again.\n") print("Bye!") Program Screenshot :
Output:
I have commented the code where I changed..
Hope you understand...
If you have any doubts comment below..plss dont dislike....