我如何退出 if 循环并进入菜单?

How do I get out from the if loop and into the menu?

所以基本上我似乎陷入了 add_movies 函数内的无限 if 循环,但我希望程序能够在需要时切换回菜单函数。 user_choice 字符串中的内容似乎并不重要,我不明白为什么。感谢您的帮助。

import sys
movies_list = []


user_key = input("To add a movie enter 'add', to quit enter 'quit'")

def menu():

  while user_key != "quit":
        if user_key == "add":
            add_movies()
        elif user_key == "list":
          pass
        elif user_key == "quit":
          sys.exit()
        else:
          pass

def add_movies():
    user_choice = input("Would you like to add a movie to the list of movies? \n\n ENTER: ")
    if user_choice == "yes":
        movie_title = input("Great! Please enter the name of the movie title: ")
        director = input("Great! Please enter the name of the movie's director: ")
        year = input("Great! Please enter the name of the movie's release date: ")
        movies_list.append(
            {
                "movie_title": movie_title,
                "director": director,
                "year": year
            }
        )
        print("_________________________________________________________________________________________________")
#It's here where i can't get to the menu again even if i type "no"
    elif user_choice == "no":
        menu()

menu()

您正在递归调用菜单。将输入提示放在while循环中。

movies_list = []
def menu():

    while (user_key := input("To add a movie enter 'add', to quit enter 'quit'\n")) != "quit":
        if user_key == "add":
            add_movies()
        elif user_key == "list":
            pass
        elif user_key == "quit":
            sys.exit()
        else:
            pass


def add_movies():
    user_choice = input(
        "Would you like to add a movie to the list of movies? \n\n ENTER: ")
    if user_choice == "yes":
        movie_title = input(
            "Great! Please enter the name of the movie title: ")
        director = input(
            "Great! Please enter the name of the movie's director: ")
        year = input(
            "Great! Please enter the name of the movie's release date: ")
        movies_list.append(
            {
                "movie_title": movie_title,
                "director": director,
                "year": year
            }
        )
        print("_________________________________________________________________________________________________")
# It's here where i can't get to the menu again even if i type "no"
    elif user_choice == "no":
        return


menu()