Function error

-1

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'
    
asked by estrellas oscuras 18.09.2018 в 02:06
source

1 answer

0

The truth is that I do not understand your code very well, I guess the logic is, but I was taught with another type of syntax to code Python in the U: p anyway I tell you the problem and write a solution but as I was taught to codear sry :( well, first of all the problem lies in that you are trying to find a key in the dictionary that you created called 'Aliens' which does not exist, now the code:

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 ")
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
})

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 show_movie(movie_list):
for k in range(0,len(movie_list)):
    show_details_movie(movie_list[k])

def find_movie():
find_by = input("What property of the movie are you looking for? ")
looking_for = input("What are you searching for? ")
found=find_by_attribute(movies, looking_for, find_by)
show_movie(found)

def find_by_attribute(items, expected, finder):
found=[]
for k in range(0,len(items)):
    dicc=items[k]
    if dicc[str(finder)]==expected:
        found.append(dicc)
return found        

menu()
    
answered by 18.09.2018 / 04:56
source