Aug-02-2020, 03:36 PM
I'm struggling with iterating over a list of dictionaries to access keys/values based on a user selection.
The first loop works as expected to print the chosen dict values for a user to review but the second one attempting to match the user input to the dictionary key to access the 'id' value within it is not working. The script ends with it printing "It failed."
Is this a data type conversion issue? What am I missing here?
The first loop works as expected to print the chosen dict values for a user to review but the second one attempting to match the user input to the dictionary key to access the 'id' value within it is not working. The script ends with it printing "It failed."
Is this a data type conversion issue? What am I missing here?
import tmdbsimple as tmdb tmdb.API_KEY = 'API KEY REMOVED FOR PUBLIC POSTING' import requests import locale search = tmdb.Search() response = search.movie(query=str(input("Enter a movie name: "))) #print(response) list1 = [] inc = 1 #generate list for user to review for s in search.results: print(str(inc) + "." + s['title'] + " " + s['release_date'] + " " + str(s['id']) + " " + "https://image.tmdb.org/t/p/original" + s['poster_path'] + " ") list1.append(s['id']) inc += 1 if inc > 5: break enumerate(list1, 1) choice = input("Choose a movie: ") choice1 = list1[int(choice)-1] #iterate over list of dictionaries again to match user choice to correct dict for t in search.results: if t['id'] == choice1: print("You chose " + str(choice1)) else: print("It failed.") break #print(list1) prints the expected list of ids