Question: PYTHON I'm having an issue with the display_minutes() function. Every time I run it I get this error line 125, in with closing(conn.cursor()) as c:

PYTHON

I'm having an issue with the display_minutes() function. Every time I run it I get this error

line 125, in with closing(conn.cursor()) as c: NameError: name 'closing' is not defined

here is the program.

def main():

db.connect() display_title() cat_menu = build_categories() while True:

command = int(input(COM_MENU)) if command == CAT: display_movies_by_category(cat_menu)

elif command == YEAR: display_movies_by_year()

elif command == MIN: display_minutes()

elif command == ADD: add_movie()

elif command == DEL: delete_movie() elif command == EXIT: break

else: print("Not a valid command. Please try again. ") display_menu() db.close() print("Bye!")

def display_title(): print("The Movie List program ")

return

def build_categories():

cat_menu = "CATEGORIES "

categories = db.get_categories() for category in categories: cat_menu += str(category.id) + ". " + category.name + " "

cat_menu += f" Please select your CATEGORY choice: (1-{category.id}): "

return cat_menu

def display_movies(movies, title_term): print("MOVIES - " + title_term) line_format = "{:>4s} {:37s} {:6s} {:5s} {:10s}" print(line_format.format("ID", "Name", "Year", "Mins", "Category")) print("-" * 65) for movie in movies: print(line_format.format(str(movie.id), movie.name, str(movie.year), str(movie.minutes), movie.category.name)) print()

def display_movies_by_category(cat_menu):

category_id = int(input(cat_menu)) category = db.get_category(category_id) if category == None: print("There is no category with that ID. ") else: print() movies = db.get_movies_by_category(category_id) display_movies(movies, category.name.upper())

def display_movies_by_year(): year = int(input("Year: ")) print() movies = db.get_movies_by_year(year) display_movies(movies, str(year))

### The display_minutes() function below ###

def display_minutes(): minutes = int(input("Enter max number of mins:" ))

query = '''SELECT movieID, Movie.name, year, minutes, Movie.categoryID as categoryID, Category.name as categoryName FROM Movie JOIN Category ON Movie.categoryID = Category.categoryID WHERE Movie.categoryID = ?'''

with closing(conn.cursor()) as c: c.execute(query, (category_id,)) results = c.fetchall()

###

def add_movie(): name = input("Name: ") year = int(input("Year: ")) minutes = int(input("Minutes: ")) category_id = int(input("Category ID: "))

category = db.get_category(category_id) if category == None: print("There is no category with that ID. Movie NOT added. ") else: movie = Movie(name=name, year=year, minutes=minutes, category=category) db.add_movie(movie) print(name + " was added to database. ")

def delete_movie(movie_id): print("Would you like to delete " + int(movie_id) + " Y or N ") if movie_id == 'N': return else: query = '''DELETE m.* FROM Movie m WHERE m.movieID <= {} '''.format(movie_id)

with closing(conn.cursor()) as c: c.execute(query)

print("Movie ID " + str(movie_id) + " was deleted from database. ")

if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!