I have a problem that I can not solve. The program works a good part, but when you add a function more find_movie (), it has the functionality to show and add the added movies plus the ones you are looking for. functions enter what the user types then print them but for some reason he does not bring them to me and he gives an error.
movies=[]
def menu():
user=input("Escriba: ingresar= a,mostrar=l, encontrar=f salir=x ")
while user != "x":
if user == "a":
add_movie()
elif user == "l":
show_movie(movies)
elif user == "f":
find_movie()
else:
print("intente otra vez")
user = input("Escriba: ingresar= a,mostrar=l, encontrar=f salir=x ")
#ingresamos mediante imputs
def add_movie():
name=input("Enter de movie name :")
autor=input("Enter de movie director :")
year=int(input("Enter de movie director :"))
movies.append({
"name":name,
"autor":autor,
"year":year
})
#para mostrar tenemos que recorrer
#el diccionario mediante for
def show_movie(movie_list):
for movie in movie_list:
show_details_movie(movie)
def show_details_movie(movie):
print(f"""this is name: {movie["name"]}""")
print(f"""this is name: {movie["autor"]}""")
print(f"""this is name: {movie["year"]}""")
def find_movie():
find_by = input("What property of the movie are you looking for? ")
looking_for = input("What are you searching for? ")
found_movies = find_by_attribute(movies, looking_for, lambda x : x[find_by])
show_movie(found_movies)
def find_by_attribute(items, expected, finder):
found=[]
for i in items:
if finder (i) == expected:
found.append(i)
return found
menu()
RESULT:
Escriba: ingresar= a,mostrar=l, encontrar=f salir=x a
Enter de movie name :Aliens
Enter de movie director :Scott
Enter de movie director :1989
Escriba: ingresar= a,mostrar=l, encontrar=f salir=x l
this is name: Aliens
this is name: Scott
this is name: 1989
Escriba: ingresar= a,mostrar=l, encontrar=f salir=x f
What property of de movie are you looking for? aliens
What are you searshing for? Scott
Traceback (most recent call last):
File "C:/Users/reset/PycharmProjects/movies.py", line 59, in <module>
menu()
File "C:/Users/reset/PycharmProjects/movies.py", line 12, in menu
find_movie()
File "C:/Users/reset/PycharmProjects/movies.py", line 44, in find_movie
found_movies = find_by_attribute(movies, looking_for, lambda x: x[find_by])
File "C:/Users/reset/PycharmProjects/movies.py", line 53, in find_by_attribute
if finder(i) == expected:
File "C:/Users/reset/PycharmProjects/movies.py", line 44, in <lambda>
found_movies = find_by_attribute(movies, looking_for, lambda x: x[find_by])
KeyError: 'aliens'