Home > Net >  Im stuck on in-completed code on printing variable and list
Im stuck on in-completed code on printing variable and list

Time:02-11

This is the code I'm stuck on. For example, if I input in Horror, Action then the output would be

Anabelle=["Horror", "Triller"]
Avengers=["Action", "Fantasy", "Sci-fi"]
Scooby_doo=["Comedy", "Horror"]
Brooklyn_99=["Action", "Comedy"]
Fast_Furious=["Action"]
Conjuring=["Horror"]
Spider_Man=["Action", "Fantasy"]

basically, printing the variable and the list that contains the user input. also if possible, I need to use lists, also don't change the code too much and add comments for each section


my_list= ["Horror, Action, Comedy, Thriller, Mystery, Fantasy, Sci-fi, Romance, 
Drama, Dystopian"]

print("All Available Genres "   str(my_list))

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"]



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']
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]


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(",")

CodePudding user response:

This is quite ugly, but I've tried not to change it too much:

my_list= ["Horror", "Action", "Comedy", "Thriller", "Mystery", "Fantasy", "Sci-fi", "Romance", "Drama", "Dystopian"]

print("All Available Genres "   str(my_list))

movies = {
    "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"],
}

user_genres = (input("What movie/show genre do you like to watch?: "))
user_genres = user_genres.split(", ") if ", " in user_genres else user_genres.split(",")

for (movie, genres) in movies.items():
    toPrint = False
    for genre in genres:
        if genre in user_genres:
            toPrint = True
    if toPrint:
        print(movie   " ["   ", ".join(genres)   "]")

CodePudding user response:

I'm not 100% sure if this is what you are asking for but here is some code that

  • -iterates through movies
  • -checks if genre is in each movie''
  • if it is, prints movie name.

Simply add this to the end of your code

user_genre_s = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")
movies_found = 0
print(f"The following movies were found for genres {user_genre}:")
for movie in newlist:
    for genre in user_genre_s:
        if genre in movie:
            print(variable_strings[newlist.index(movie)].replace("_"," "))
            movies_found  = 1
if movies_found == 0:
    print(":( no match was found")

However you may want to lower() all of the genres as users may not enter capitals

  • Related