我在哪里遇到简单的“搜索/打印功能”?

时间:2018-06-06 19:45:14

标签: search printing find

我正在使用Python训练营,并且一直在尝试跟进,但我怀疑我在努力使程序更加健壮的时候有点偏离脚本...但现在我不能得到dang的东西“找电影”,然后“显示电影”...它只是恢复到“选择?:”输入请求...我敢肯定它是一个小的东西,如缩进,但我不能发现代码有问题......

movies = []


def menu():
    global user_input
    print("\nWelcome to the Movie Program Database!")
    print("\nWhich function would you like to do?:")
    print("(A)dd movie to your list")
    print("(L)ist movies you've added")
    print("(S)earch for movies in your list\n")
    user_input = str(input("Selection: ").capitalize())
    while user_input != 'Q':
        if user_input == 'A':
            add_movies()
        elif user_input == 'L':
            show_movies(show_movie_details)
        elif user_input == 'S':
            find_movie()
        else:
            print("\n\n--Unknown command--Please try again.\n")
            print("Which function would you like to do?:\n\n")
            print("\n(A)dd movie to your list")
            print("(L)ist movies you've added")
            print("(S)earch for movies in your list")
        user_input = str(input("Selection: ").capitalize())


def command_user():
    global user_input
    print("\nWhich function would you like to do?:\n")
    print("(A)dd movie to your list")
    print("(L)ist movies you've added")
    print("(S)earch for movies in your list\n")
    user_input = str(input("Selection: ").capitalize())
    while user_input != 'Q':
        if user_input == 'A':
            add_movies()
        elif user_input == 'L':
            show_movies(movies)
        elif user_input == 'S':
            find_movie()
        else:
            print("\n\n--Unknown command--Please try again.\n")
            print("\nWhich function would you like to do?:\n")
            print("(A)dd movie to your list")
            print("(L)ist movies you've added")
            print("(S)earch for movies in your list\n")
        user_input = str(input("Selection: ").capitalize())


def add_movies():
    global user_input

    while True:
        global year
        year = False
        name = (input('What is the title of the movie?: ').title())
        director = str(input("Who was the director of this movie?: ").title())
        while True:
            year = input("Release Date? (or enter '?'): ")
            if year == "?":
                break
            else:
                try:
                    year = int(year)
                    break
                except ValueError:
                    print("That's not a date, try again!")
                    continue
        movies.append({
            "name": name,
            "director": director,
            "year": year
        })
        command_user()


def show_movies(movies_list):
    for movie in movies_list:
        show_movie_details(movie)


def show_movie_details(movie):
        print(f"Name: {movie['name']}")
        print(f"Director: {movie['director']}")
        print(f"Release Year: {movie['year']}\n")


def find_movie():
    print("Which property are you looking for? a 'Name', 'Director', or 'Year'")
    find_by = input("Selection?: ")
    looking_for = input("What is it you're searching for?: ")

    found_movies = find_by_attribute(movies, looking_for, lambda x: x[find_by])

    show_movies(found_movies)


def find_by_attribute(items, expected, finder):
    found = []

    for i in items:
        if finder(i) == expected:
                found.append(i)

    return found


menu()

0 个答案:

没有答案