求助:Python电影列表程序my_list函数未定义,如何定义movie_list功能?
Python电影列表程序问题修正
问题描述
我编写的Python程序需要实现三个功能:查看电影列表、添加电影至列表、从列表中删除电影。目前菜单功能可正常运行,但输入"list"命令时始终触发函数未定义的报错,且不清楚如何正确定义显示电影列表的函数。以下是当前代码:
def main(): movie_list = [["Spirited Away"],["Whiplash"],["The Dark Night"]] #Menu where you can select to view the list, add a movie to the list, delete a movie from the list, or exit the program. def movie_menu(): print("Movie Menu") print("list-Display all movies") print("add-Add a movie") print("del-Delete a movie") print("exit-Exit program") #Program where you type the commands to "add","delete","exit",or "view" the list. movie_menu() while True: command = input("Command:") if command == "list": list(movie_list) elif command == "add": add(movie_list) elif command == "del": delete(movie_list) elif command == "exit": break else: print("Not a valid command. Please try again.") print("Goodbye!!") **def list(movie_list): for i in range(len(movies)): print(str(i+1) + "." + movies[i]) - The code I need help on print(movie_list) ** def add(movie_list): name = input("Name:") movie = [] movie.append(name) movie_list.append(movie) print(movie[0] + "was added.\n") def delete (movie_list): num = int(input("Number:")) if number < 1 or number > len(movie_list): print("Invalid movie number.\n") else: movie = movie_list.pop(number-1) print(movie[0] + "was deleted.\n") main() list(movie_list)
错误分析与修正点
- 函数名冲突:用
list作为自定义函数名会覆盖Python内置的list()函数,导致调用混乱,需改为如show_movie_list的名称。 - 变量作用域问题:
movie_list定义在main()内部,属于局部变量,外部无法直接访问,需改为全局变量或调整流程管理。 - 函数定义位置错误:自定义
list、add、delete函数定义在while循环之后,导致调用时函数未定义,需调整到调用代码之前。 - 变量名错误:
delete函数中用num接收输入,但判断时用了未定义的number;显示函数中参数是movie_list,但循环里用了未定义的movies。 - 字符串拼接问题:添加/删除提示字符串缺少空格,导致输出格式混乱,需补充空格。
- 嵌套列表处理:原电影列表是嵌套结构,显示时需要取
movie[0]才能拿到电影名称。
修正后的完整代码
# 初始化全局电影列表 movie_list = [["Spirited Away"], ["Whiplash"], ["The Dark Knight"]] def movie_menu(): print("Movie Menu") print("list-Display all movies") print("add-Add a movie") print("del-Delete a movie") print("exit-Exit program") def show_movie_list(): # 遍历电影列表并格式化输出 for idx, movie in enumerate(movie_list, start=1): print(f"{idx}. {movie[0]}") def add_movie(): name = input("Name: ") movie_list.append([name]) print(f"{name} was added.\n") def delete_movie(): try: num = int(input("Number: ")) if 1 <= num <= len(movie_list): deleted_movie = movie_list.pop(num-1) print(f"{deleted_movie[0]} was deleted.\n") else: print("Invalid movie number.\n") except ValueError: print("Please enter a valid number.\n") # 程序主流程 movie_menu() while True: command = input("Command: ").strip().lower() if command == "list": show_movie_list() elif command == "add": add_movie() elif command == "del": delete_movie() elif command == "exit": break else: print("Not a valid command. Please try again.") print("Goodbye!!")
修正说明
- 重命名自定义列表显示函数为
show_movie_list,避免与内置函数冲突。 - 使用
enumerate简化遍历逻辑,直接获取索引和电影元素。 - 修复变量名错误,统一命名规范。
- 增加异常处理,防止输入非数字导致程序崩溃。
- 调整代码结构,确保函数定义在调用之前。
- 修复字符串拼接的空格问题,提升提示信息可读性。
内容的提问来源于stack exchange,提问作者Michael Pepprah
相关产品推荐
相关产品推荐

