I'm new to python and im attempting for my program to output a the variable "movie genre" vertically once a user enters their name. I plan to use a while loop but keep hitting the error "movie_genre". And perplexed as to how to proceed.
def main():
#Movie list
movie_genre = ["Action", ["Horror"], ["Adventure"], ["Musical"], ["Comedy"], ["Sci-Fi"], ["Drama"], ["Romance"], ["Thriller"]]
name = ''
#We introduce the program to the user
print ("Hello my name is Frank, the theatre clerk!")
#Asks the user to input their name
print ("May I please know who i have the pleasure of speaking with?")
#User submits name
name = input("")
#Returns user name prompt
print(name ", pleasure to make your acquaintance!")
while name:
#Asks the user for what genre they want to watch
i = name ("What are we interested in watching today?")
for x in zip (*movie_genre):
print (x)
CodePudding user response:
movie_genre
isn't defined in the same scope as where you are trying to use it.
This can be fixed in a variety of ways, either indent all of your code so it is under the same scope as the main function, and then call that function using main()
Or
A simpler fix is to just move the definition of movie_genre
closer to where you will use it:
movie_genre = [["Action"], ["Horror"], ["Adventure"], ["Musical"], ["Comedy"], ["Sci-Fi"], ["Drama"], ["Romance"], ["Thriller"]]
if name:
#Asks the user for what genre they want to watch
print("What are we interested in watching today?")
for x in zip(*movie_genre):
print(x)
CodePudding user response:
You didn't show error message but if this is your original indentations then problem is that you create movie_genre
inside function - so it is local variable which exists only inside function, but rest of code is outside function and it can't access it. You should move all code inside function or you should keep all code outside function.
I will keep all outside function - so I can remove it.
There are other mistakes and few elements which you could fix
You could keep genders withou inner []
and then you don't need zip(*...)
movie_genre = ["Action", "Horror", "Adventure", "Musical", "Comedy", "Sci-Fi", "Drama", "Romance", "Thriller"]
for genre in movie_genre:
print(genre)
You use name
in strange way in i = name(..)
- name
is a string
and you can't use it like a function. Maybe you needed input()
to ask for selected genre
- selected = input(...)
- but I would do this after displaying genres.
I also don't know what you want to do with while name
. This will run loop forever because you doesn't change name
inside loop. Maybe you need something different - ie. maybe you want to repeate loop until user select correct gender - `while selected not in movie_genre:
Full example
#Movie list
movie_genre = ["Action", "Horror", "Adventure", "Musical", "Comedy", "Sci-Fi", "Drama", "Romance", "Thriller"]
name = ''
#We introduce the program to the user
print("Hello my name is Frank, the theatre clerk!")
#Asks the user to input their name
print("May I please know who i have the pleasure of speaking with?")
#User submits name
name = input()
#Returns user name prompt
print(name ", pleasure to make your acquaintance!")
# --- select genre ---
selected = "" # default value before loop
while selected not in movie_genre: # repeate until user select genre
for genre in movie_genre:
print(genre)
#Asks the user for what genre they want to watch
selected = input("What are we interested in watching today?")