Home > other >  How to make "movie_list" function be defined
How to make "movie_list" function be defined

Time:11-02

*For my code I have three options: View a list of movies, Add to the list, or delete from the list. My problem is I don't know how to define my movie_list function. Down below you can see what I have written down so far. The menu works but for whenever I type list I keep getting an error my_list function is not defined. *

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)


CodePudding user response:

list() is already a function in python. When you code calls list(movie_list) it is accessing a list called movie_list then does nothing because you are not doing anything with it. The next thing you will want to do its move your functions to the top of your code. Python runs one line at a time, and your functions are not created when you call them in your code.

CodePudding user response:

The following codes show how to view a list of movies. enter image description here

  • Related