*对于我的代码,我有三个选项:查看电影列表、添加到列表中或从列表中删除。我的问题是我不知道如何定义我的movie_list函数。在下面你可以看到我到目前为止所写的东西。该菜单可以工作,但是每当我输入list时,我总是会得到一个错误的my_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)发布于 2022-11-01 21:44:57
list()已经是python中的一个函数。当您编写调用列表( movie_list )时,它正在访问一个名为movie_list的列表,然后什么也不做,因为您没有对它做任何事情。接下来要做的事情是将函数移到代码的顶部。Python一次运行一行,在代码中调用函数时不创建函数。
https://stackoverflow.com/questions/74281878
复制相似问题