首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何定义"movie_list“函数

如何定义"movie_list“函数
EN

Stack Overflow用户
提问于 2022-11-01 21:15:54
回答 1查看 37关注 0票数 0

*对于我的代码,我有三个选项:查看电影列表、添加到列表中或从列表中删除。我的问题是我不知道如何定义我的movie_list函数。在下面你可以看到我到目前为止所写的东西。该菜单可以工作,但是每当我输入list时,我总是会得到一个错误的my_list函数。*

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-11-01 21:44:57

list()已经是python中的一个函数。当您编写调用列表( movie_list )时,它正在访问一个名为movie_list的列表,然后什么也不做,因为您没有对它做任何事情。接下来要做的事情是将函数移到代码的顶部。Python一次运行一行,在代码中调用函数时不创建函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74281878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档