Skip to content
Advertisement

Using a while loop to output a list after user input

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)

Advertisement

Answer

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 genreselected = 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?")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement