I have to solve an exercise for school, but I cant solve it. The task is the following:
Define a function "x" that takes a catalog like the previous one and a list of favorite topics, and generates suggestions for movies that touch on any of those topics.
The catalog is this one:
example = [
{"title": "The Hard Way", "themes": ["Action & Adventure"]},
{"title": "Asmaa", "themes": ["Dramas", "International Movies"]},
{"title": "Dukhtar", "themes": ["Dramas", "Independent Movies", "International Movies"]},
{"title": "The Bachelorette", "themes": ["Reality TV", "Romantic TV Shows"]},
{"title": "12 Years Promise", "themes": ["International TV Shows", "Korean TV Shows", "Romantic TV Shows"]},
{"title": "Alexandria: Again and Forever", "themes": ["Classic Movies", "Dramas", "International Movies"]},
{"title": "Road Trip: Beer Pong", "themes": ["Comedies"]},
{"title": "Chashme Buddoor", "themes": ["Comedies", "International Movies", "Music & Musicals"]},
{"title": "House Party 3", "themes": ["Comedies", "Music & Musicals"]},
{"title": "Manhunt", "themes": ["Action & Adventure", "International Movies"]}
]
This is what I coded so far:
def suggestions(catalog, topics):
recomended = []
for element in catalog:
if topics[0] in element["themes"]:
recomended.append(element["title"])
return recomended
it returns ['Asmaa', 'Dukhtar', 'Alexandria: Again and Forever'], but it should return ["Asmaa", "Dukhtar", "12 Years Promise", "Alexandria: Again and Forever"].
I believe the problem is because i just check the first element from topics, but I don't know how to check all the elements from topics.
CodePudding user response:
You probably need a skeleton program as a kick-start.
Minimal reproducible example
I added some comments to help and guide.
# your function x, but renamed and properly indented with 4 spaces
def recommend_for(catalog,list): # rename the parameter from list to e.g. topics
recomended = [] # your resulting list, append to it
for element in catalog:
if list[0] in element["themes"]: # only the first topic should match
list.append(recomended, element["title"]) # consult the python docs
return recomended
# given
catalog = [] # your example defined here as list
topics = []
# when
result = recommend_for(catalog, topics) # call of function x
# then
print(result) # test if the result is as expected
Note: I renamed x
to recommend_for
to clearly show what the method is expected to do and to return
Next steps
The implementation of your function x had and still has many issues (indentation, naming, append()
syntax, correctness of if-condition).
Try to solve them one by one and test your code. As soon as you can present us a minimal-reproducible-example and ask a specific question for your issue (with error-message or un-expected result) we can help.
CodePudding user response:
You should add similar genres to "recomended", not to "list". Try this:
example = [
{"title": "The Hard Way", "themes": ["Action & Adventure"]},
{"title": "Asmaa", "themes": ["Dramas", "International Movies"]},
{"title": "Dukhtar", "themes": ["Dramas", "Independent Movies", "International Movies"]},
{"title": "The Bachelorette", "themes": ["Reality TV", "Romantic TV Shows"]},
{"title": "12 Years Promise", "themes": ["International TV Shows", "Korean TV Shows", "Romantic TV Shows"]},
{"title": "Alexandria: Again and Forever", "themes": ["Classic Movies", "Dramas", "International Movies"]},
{"title": "Road Trip: Beer Pong", "themes": ["Comedies"]},
{"title": "Chashme Buddoor", "themes": ["Comedies", "International Movies", "Music & Musicals"]},
{"title": "House Party 3", "themes": ["Comedies", "Music & Musicals"]},
{"title": "Manhunt", "themes": ["Action & Adventure", "International Movies"]}
]
def lookforsimilar(catalog, mylist):
recomended = []
for element in catalog:
if mylist[0] in element["themes"]:
recomended.append([element["title"]])
return recomended
myfilm = ["Comedies", "I Want You Back"] # genre, name
mynext = lookforsimilar(example, myfilm)
print(mynext)
# [['Road Trip: Beer Pong'], ['Chashme Buddoor'], ['House Party 3']]
! Best practice is not to use keywords like "list" as names.