Hi there: i have this code from my tutorial that i want variations to help me learn
I am having difficulties in understanding some parts of it. the part that has
"def print_movie()"" has just jammed up my complete understanding of the code.
i will need some other samples that achieve this same results to help me get the concept:
the mission of the the code is to allow me create a database of movies,
be able to store new movies, search the movies by title or by year or by director,
it should allow me show all the list in the library or exit when certain keys are entered.```
Code:
start_menu = "\nEnter lower case A, S, F or Q to operate the software:"
movies = []
def add_movie():
title = input("Enter your tile here: ")
director = input("Type in director: ")
year = input("Enter the year: ")
# add_movie()
movies.append({
'title': title,
'director': director,
'year': year
})
def show_movies():
print("Your movie list:")
for movie in movies:
**print_movie(movie)**
this print_movie function has really mess my mind up and am unable to figure its essence.
**def print_movie(movie):**
** print(f"Title: {movie['title']}")**
** print(f"Director: {movie['director']}")**
** print(f"Release year: {movie['year']}")**
def find_movie():
search_title = input("Enter movie title you looking for: ")
for movie in movies:
if movie['title'] == search_title:
print_movie(movie)
def menu():
selection = input(start_menu)
while selection != 'q':
if selection == 'a':
add_movie()
elif selection == 's':
show_movies()
elif selection == 'f':
find_movie()
else:
print('Unknown command please try again.!')
selection = input(start_menu)
menu()
This is simply a working hypothesise…
Maybe you could have something along the lines of:
class movie:
def __init__(self, title, director, year):
self.title = title
self.director = director
self.year = year
def add_movie():
title = input("Enter your tile here: ")
director = input("Type in director: ")
year = input("Enter the year: ")
key = title.replace(" ", "_")
movies[key] = movie(title, director, year)
def show_movies():
output = ''
print("Your movie list:")
for entry in movies:
output += f"Title: {movies[entry].title}\n"
output += f"Director: {movies[entry].director}\n"
output += f"Release year: {movies[entry].year}\n"
print(output)
movies = {}
To list all the movies, you simply call show_movies()
As for adding movies: although this is a manual process as is, I see no reason for it being so…
The add_movie()
function could read a .csv
file and as such any new movies added to said file would be added to movies = {}
I’ve not taken this any further, but I hope it’s of some use.
Footnote: please don’t format the text in your posts as a ‘code block’; it makes reading your posts that much more of an effort.
The new add_movie():
function:
import csv
...
def add_movie():
with open('movies.csv', mode='r', encoding='UTF-8') as data:
read = csv.reader(data)
for entry in read:
title = entry[0]
director = entry[1]
year = entry[2]
key = title.replace(" ", "_")
movies[key] = movie(title, director, year)
Having come back to this I feel that the add_movie()
function should be renamed to load_movies()
(because that is what it’s doing) and code another function called add_movie()
for that operation.
I have done that, but I don’t want to take all the fun out of this for you, so unless you want me to post up what I have, I’ll not do so.
The other thing I’ve done is to extend the show_movies()
function with a search option, so that (for example) running show_movies('Brian')
will display only the movies that have that association…
Listings for search term 'Brian'...
Title: Scarface
Director: Brian De Palma
Release year: 1983
Title: Monty Python's Life of Brian
Director: Terry Jones
Release year: 1979
Again, I’ll post up the code ONLY upon request.
First, please note that it does not work to try to highlight code or put it in boldface. Instead, you should write the post so that it is clear what part of the code you are talking about. (One thing that helps is to be more selective about the code that you show. Here is some advice: How to create a Minimal, Reproducible Example.) It’s also a good idea to make sure that your text explaining the problem is separate from the formatted code. When you use ``` to format the code, make sure it is above and below just the code part, on a line by itself both above and below.
Aside from that: I cannot understand what confuses you about the code. For example, if I showed you print(f"Title: {movie['title']}")
by itself, would you understand that? How about just the f"Title: {movie['title']}"
part? How about just the movie['title']
part?