Question: Add data validation to the add_movie() function so the year entry is a valid integer thats greater than zero. Then, test this change. Modify the
Add data validation to the add_movie() function so the year entry is a valid integer thats greater than zero. Then, test this change.
Modify the write_movies() function so it also handles any OSError exceptions by displaying the class name and error message of the exception object and exiting the program.
Test this by using a raise statement in the try block that raises a BlockingIOError. This is one of the child classes of the OSError. Then, comment out the raise statement.
In the read_movies() function, comment out the two statements in the except clause for the FileNotFoundError. Instead, use this except clause to return the empty movies list thats initialized in the try block. This should cause the program to continue if the file cant be found by allowing the program to create a new file for the movies that the user adds.
Test this by first running the program with the movies.csv file. That should work as before. Then, change the filename in the global constant to movies_test.csv, run the program again, and add a movie. That should create a new file. If that works, run the program again to check whether it works with the new file.
Create a banner at the start of application
========== My Movie Application ==========
Starting Code
import csv
import sys
FILENAME = "movies.csv"
def exit_program():
print("Terminating program.")
sys.exit()
def read_movies():
try:
movies = []
with open(FILENAME, newline="") as file:
reader = csv.reader(file)
for row in reader:
movies.append(row)
return movies
except FileNotFoundError as e:
print(f"Could not find {FILENAME} file.")
exit_program()
except Exception as e:
print(type(e), e)
exit_program()
def write_movies(movies):
try:
with open(FILENAME, "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(movies)
except Exception as e:
print(type(e), e)
exit_program()
def list_movies(movies):
for i, movie in enumerate(movies, start=1):
print(f"{i}. {movie[0]} ({movie[1]})")
print()
def add_movie(movies):
name = input("Name: ")
year = input("Year: ")
movie = [name, year]
movies.append(movie)
write_movies(movies)
print(f"{name} was added. ")
def delete_movie(movies):
while True:
try:
number = int(input("Number: "))
except ValueError:
print("Invalid integer. Please try again.")
continue
if number < 1 or number > len(movies):
print("There is no movie with that number. Please try again.")
else:
break
movie = movies.pop(number - 1)
write_movies(movies)
print(f"{movie[0]} was deleted. ")
def display_menu():
print("The Movie List program")
print()
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():
display_menu()
movies = read_movies()
while True:
command = input("Command: ")
if command.lower() == "list":
list_movies(movies)
elif command.lower() == "add":
add_movie(movies)
elif command.lower() == "del":
delete_movie(movies)
elif command.lower() == "exit":
break
else:
print("Not a valid command. Please try again. ")
print("Bye!")
if __name__ == "__main__":
main()
PLEASE copy paste changed code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
