This is my program where the user would input a genre and would output movie/movies and if they input "Stop" then the program would print something but when I try to run it, it says that "user_genre" was not defined even though it is.
Anabelle=["Horror", "Triller"]
Criminal_Minds=["Mystery", "Drama"]
Avengers=["Action", "Fantasy", "Sci-Fi"]
Scooby_doo=["Comedy", "Horror"]
Brooklyn_99=["Action", "Comedy" ]
The_fault_in_our_stars=["Romance", "Drama"]
The_tomorrow_war=["Drama", "Sci-Fi"]
Maze_Runner=["Drama", "Dystopian", "Thriller"]
Hunger_Game=["Dystopian", "Sci-Fi", "Thriller"]
Harry_Potter=["Mystery", "Fantasy"]
Fast_Furious=["Action"]
Conjuring=["Horror"]
Fantastic_Beast=["Fantasy"]
Parasite=["Comedy", "Thriller"]
Space_between_us=["Romance"]
Murder_Mystery=["Romance", "Mystery"]
The_Purge=["Dystopian", "Thriller"]
Spider_Man=["Action", "Fantasy"]
Dune=["Action", "Dystopian"]
Terminator=["Action", "Sci-Fi"]
Tom_and_Jerry=["Comedy"]
Dolittle=["Comedy", "Fantasy"]
variable_strings=['Anabelle', 'Criminal_Minds', 'Avengers', 'Scooby_doo', 'Brooklyn_99', 'The_fault_in_our_stars', 'The_tomorrow_war',
'Maze_Runner', 'Hunger_Game', 'Harry_Potter', 'Fast_Furious', 'Conjuring', 'Fantastic_Beast', 'Parasite',
'Space_between_us', 'Murder_Mystery', 'The_Purge', 'Spider_Man', 'Dune', 'Terminator', 'Tom_and_Jerry', 'Dolittle']
newlist = [Anabelle, Criminal_Minds, Avengers, Scooby_doo, Brooklyn_99, The_fault_in_our_stars, The_tomorrow_war,
Maze_Runner, Hunger_Game, Harry_Potter, Fast_Furious, Conjuring, Fantastic_Beast, Parasite, Space_between_us, Murder_Mystery, The_Purge, Spider_Man, Dune, Terminator, Tom_and_Jerry, Dolittle]
def ask():
user_genre = (input("What movie/show genre do you like to watch?: "))
user_genre = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")
movies_found = 0
print("The following movies were found for the genres" str(user_genre))
for movie in newlist:
for genre in user_genre:
if genre in movie:
print(variable_strings[newlist.index(movie)].replace("_"," "))
movies_found = 1
if movies_found == 0:
print("No match was found")
def stop_running():
while user_genre != "Stop":
ask()
if user_genre == "Stop":
print("Stopping")
stop_running()
CodePudding user response:
This is because you are defining user_genre
in ask()
function. You are already using the user_genre
before running the ask()
function.
You can remove the stop_running()
function and apply that logic in the ask()
function itself. This can be achieved in the following way:
Anabelle=["Horror", "Triller"]
Criminal_Minds=["Mystery", "Drama"]
Avengers=["Action", "Fantasy", "Sci-Fi"]
Scooby_doo=["Comedy", "Horror"]
Brooklyn_99=["Action", "Comedy" ]
The_fault_in_our_stars=["Romance", "Drama"]
The_tomorrow_war=["Drama", "Sci-Fi"]
Maze_Runner=["Drama", "Dystopian", "Thriller"]
Hunger_Game=["Dystopian", "Sci-Fi", "Thriller"]
Harry_Potter=["Mystery", "Fantasy"]
Fast_Furious=["Action"]
Conjuring=["Horror"]
Fantastic_Beast=["Fantasy"]
Parasite=["Comedy", "Thriller"]
Space_between_us=["Romance"]
Murder_Mystery=["Romance", "Mystery"]
The_Purge=["Dystopian", "Thriller"]
Spider_Man=["Action", "Fantasy"]
Dune=["Action", "Dystopian"]
Terminator=["Action", "Sci-Fi"]
Tom_and_Jerry=["Comedy"]
Dolittle=["Comedy", "Fantasy"]
variable_strings=['Anabelle', 'Criminal_Minds', 'Avengers', 'Scooby_doo', 'Brooklyn_99', 'The_fault_in_our_stars', 'The_tomorrow_war',
'Maze_Runner', 'Hunger_Game', 'Harry_Potter', 'Fast_Furious', 'Conjuring', 'Fantastic_Beast', 'Parasite',
'Space_between_us', 'Murder_Mystery', 'The_Purge', 'Spider_Man', 'Dune', 'Terminator', 'Tom_and_Jerry', 'Dolittle']
newlist = [Anabelle, Criminal_Minds, Avengers, Scooby_doo, Brooklyn_99, The_fault_in_our_stars, The_tomorrow_war,
Maze_Runner, Hunger_Game, Harry_Potter, Fast_Furious, Conjuring, Fantastic_Beast, Parasite, Space_between_us, Murder_Mystery, The_Purge, Spider_Man, Dune, Terminator, Tom_and_Jerry, Dolittle]
def ask():
while True:
user_genre = input("What movie/show genre do you like to watch?: ")
if user_genre != "Stop":
user_genre = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")
movies_found = 0
print("The following movies were found for the genres" str(user_genre))
for movie in newlist:
for genre in user_genre:
if genre in movie:
print(variable_strings[newlist.index(movie)].replace("_"," "))
movies_found = 1
if movies_found == 0:
print("No match was found")
else:
print("stopping")
break
ask()
Output:
What movie/show genre do you like to watch?: Horror
The following movies were found for the genres['Horror']
Anabelle
Scooby doo
Conjuring
What movie/show genre do you like to watch?: Stop
stopping
CodePudding user response:
The main problem here is scope. Any variable you define in a function will have what is called local scope, meaning it exists only in that function. Each call will define it and it will cease to exist at the end of each call. Variables defined outside of any function are global variables, meaning they exist everywhere.
Note that if you define a variable within a function that has the same name as a global variable, it does not affect the global variable. It creates a new variable of the same name.
Where that comes in here is that you are trying to call user_genre
in stop_running
when that variable only exists in ask
. If you want user_genre
to be used by both functions, you need to make it global. You must declare it as global in both functions using the global
keyword-- a variable defined with this keyword is always global wherever is is defined. Note that you must declare the variable as global before defining it, in two separate lines, e.g.:
global a
a = 2
You will need to define it in stop_running
after declaring it as global there, since you are asking for a value from it in that function; it needs to have a value before ask
is called for the first time.
The final issue is that in ask
you split
user_genre
, turning it into a list instead of a string. Then when stop_running
checks it against "Stop"
it will always return False
since if the user enters Stop
then user_input
won't end up as "Stop"
but as ["Stop"]
. You can remedy this by checking if user_genre
contains "Stop"
using the in
keyword rather than checking for equality.
So your functions will look like this:
def ask():
global user_genre
user_genre = (input("What movie/show genre do you like to watch?: "))
...
def stop_running():
global user_genre
user_genre = ''
while not "Stop" in user_genre:
ask()
if "Stop" in user_genre:
print("Stopping")
Notice that it still runs the check for movies when the user enters Stop
, you can avoid that by putting
if "Stop" in user_genre:
return
in ask
before it checks for movies.
Then with all these changes the script looks like this:
Anabelle=["Horror", "Triller"]
Criminal_Minds=["Mystery", "Drama"]
Avengers=["Action", "Fantasy", "Sci-Fi"]
Scooby_doo=["Comedy", "Horror"]
Brooklyn_99=["Action", "Comedy" ]
The_fault_in_our_stars=["Romance", "Drama"]
The_tomorrow_war=["Drama", "Sci-Fi"]
Maze_Runner=["Drama", "Dystopian", "Thriller"]
Hunger_Game=["Dystopian", "Sci-Fi", "Thriller"]
Harry_Potter=["Mystery", "Fantasy"]
Fast_Furious=["Action"]
Conjuring=["Horror"]
Fantastic_Beast=["Fantasy"]
Parasite=["Comedy", "Thriller"]
Space_between_us=["Romance"]
Murder_Mystery=["Romance", "Mystery"]
The_Purge=["Dystopian", "Thriller"]
Spider_Man=["Action", "Fantasy"]
Dune=["Action", "Dystopian"]
Terminator=["Action", "Sci-Fi"]
Tom_and_Jerry=["Comedy"]
Dolittle=["Comedy", "Fantasy"]
variable_strings=['Anabelle', 'Criminal_Minds', 'Avengers', 'Scooby_doo', 'Brooklyn_99', 'The_fault_in_our_stars', 'The_tomorrow_war',
'Maze_Runner', 'Hunger_Game', 'Harry_Potter', 'Fast_Furious', 'Conjuring', 'Fantastic_Beast', 'Parasite',
'Space_between_us', 'Murder_Mystery', 'The_Purge', 'Spider_Man', 'Dune', 'Terminator', 'Tom_and_Jerry', 'Dolittle']
newlist = [Anabelle, Criminal_Minds, Avengers, Scooby_doo, Brooklyn_99, The_fault_in_our_stars, The_tomorrow_war,
Maze_Runner, Hunger_Game, Harry_Potter, Fast_Furious, Conjuring, Fantastic_Beast, Parasite, Space_between_us, Murder_Mystery, The_Purge, Spider_Man, Dune, Terminator, Tom_and_Jerry, Dolittle]
def ask():
global user_genre
user_genre = (input("What movie/show genre do you like to watch?: "))
user_genre = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")
if "Stop" in user_genre:
return
movies_found = 0
print("The following movies were found for the genres" str(user_genre))
for movie in newlist:
for genre in user_genre:
if genre in movie:
print(variable_strings[newlist.index(movie)].replace("_"," "))
movies_found = 1
if movies_found == 0:
print("No match was found")
def stop_running():
global user_genre
user_genre = ''
while not "Stop" in user_genre:
ask()
if "Stop" in user_genre:
print("Stopping")
stop_running()