In: Computer Science
Could someone please tell me what corrections I should make to this code. (Python)
Here are the instructions.
I can't get the find function to work and I have no idea how to even go about it. For example, when type in 'find' and I enter the rating, I'm always getting an error. I need help.
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]) + ")"+","+
"$"+str(row[2])+","+str(row[3]))
i += 1
print()
def add(movie_list):
name = input("Name: ")
year = input("Year: ")
price = int(input("Price:"))#price
rating =input("Rating:")
movie = []
movie.append(name)
movie.append(year)
movie.append(price)#adding price to the movie list
movie.append(rating)#adding rating to the movie list
movie_list.append(movie)
rating_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")
#find by rating function
def find_by_rating(movie_list,rating):
if len(movie_list)==0:
print("Find")
return
else:
movie_list=[]
for i in movie_list:
if i[3]==rating:
movie_list.append(i[0])
if len(1)==0:
print("No movies are present with given rating")
else:
print("Movies:")
for i in movie_list:
print(i)
def display_menu():
print("COMMAND MENU")
print("list - List all movies")
print("add - Add a movie")
print("del - Delete a movie")
print("find- find movie by rating")
print("exit - Exit program")
print()
def main():
movie_list = [["Matrix",1999,9.75,"R"],
["Under the Tuscan",2003,4.99,"PG"],
["V for Vendetta", 2005,14.99,"R"]]
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=input("Enter rating:")#taking the rating
find_by_rating(movie_list,rating)#the call
elif command == "exit":
break
else:
print("Not a valid command. Please try again.\n")
print("Bye!")
if __name__ == "__main__":
main()
Here is the code in python for the given problem.
Some corrections are done to the code provided in the question so that the program works as expected.
Some comments are added to the find() function (asked in the question) for better understanding.
Sample output is added at the end.
Code:
def list_movies(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]) + ")"+","+ "$"+str(row[2])+","+str(row[3]))
i += 1
print()
def add(movie_list):
name = input("Name: ")
year = input("Year: ")
price = float(input("Price:"))#price
rating =input("Rating:")
movie = []
movie.append(name)
movie.append(year)
movie.append(price)#adding price to the movie list
movie.append(rating)#adding rating to the 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")
#find by rating function
def find_by_rating(movie_list,rating):
if len(movie_list)==0: #checks if movie_list is empty
print("There are no movies in the list")
return
else:
movie_list_by_rating=[] #creates a new list
for i in movie_list:
if i[3]==rating: #checks if the current movie rating is equal to the rating passed as parameter
movie_list_by_rating.append(i[0]) #append movie to movie_list_by_rating list
if len(movie_list_by_rating)==0: #checks if movie_list_by_rating is empty
print("No movies are present with given rating")
else:
print("Movies:")
for i in movie_list_by_rating:
print(i) #prints movies with rating passed as parameter
def display_menu():
print("COMMAND MENU")
print("list - List all movies")
print("add - Add a movie")
print("del - Delete a movie")
print("find- find movie by rating")
print("exit - Exit program")
print()
def main():
movie_list = [["Matrix",1999,9.75,"R"],
["Under the Tuscan",2003,4.99,"PG"],
["V for Vendetta", 2005,14.99,"R"]]
display_menu()
while True:
command = input("Command: ")
if command == "list":
list_movies(movie_list)
elif command == "add":
add(movie_list)
elif command == "del":
delete(movie_list)
elif command == "find": #added find command
rating=input("Enter rating:")#taking the rating
find_by_rating(movie_list,rating)#the call
elif command == "exit":
break
else:
print("Not a valid command. Please try again.\n")
print("Bye!")
if __name__ == "__main__":
main()
Sample output: