Python Forum

Full Version: Why am I being prompted to exit the program twice?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For some reason, after exiting the while true loop with the exit choice, my program makes me exit twice (see the image i attached).
[attachment=356]
Here's the code:
#!/usr/bin/env python3
#MovieList_CSV.py
import csv

FILENAME = "movies.csv"

def writeMoviesToCSV(movieList):
    with open(FILENAME, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerows(movieList)

def readMoviesFromCSV():
    movieList = []
    with open(FILENAME, newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            movieList.append(row)
    print(movieList)

def displayMenu():
    print("COMMAND MENU")
    print("list    - List all movies")
    print("add     - Add a movie")
    print("delete  - Delete a movie")
    print("exit    - Exit")
    print()

def listTheMovies(movieList):
    if len(movieList) == 0:
        print("There are no movies in the list.\n")
        return
    else:
        i = 1
        for row in movieList:
            print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")" +
                  " $" + str(round(row[2], 2)))
            i += 1
        print()

def addMovieToList(movieList):
    name = input("Name: ")
    year = int(input("Year: "))
    price = float(input("How much should the movie ticket cost "
                        + "(don't use the $ sign): "))
    movie = []
    movie.append(name)
    movie.append(year)
    movie.append(price)
    movieList.append(movie)
    print(movie[0] + " was added.\n")
    writeMoviesToCSV(movieList)

def delete(movieList):
    number = int(input("Number: "))
    if number < 1 or number > len(movieList):
        print("Invalid movie number.\n")
    else:
        movie = movieList.pop(number-1)
        print(movie[0] + " was deleted.\n")
    writeMoviesToCSV(movieList)

def main():
    movieList = [["Waterboy", 1997, 5.00],
                 ["Expendables", 2008, 5.00],
                 ["Beavis and Butthead Do America", 1996, 20.00]]

    displayMenu()

    while True:
        command = input("Enter a command: ")
        if command.lower() == "list":
            listTheMovies(movieList)
        elif command.lower() == "add":
            addMovieToList(movieList)
        elif command.lower() == "delete":
            deleteMovieFromList(movieList)
        elif command.lower() == "exit":
            break
        else:
            print("Invalid command. Please try again.\n")

    print("And from the CSV file, the list of movies would be:")
    readMoviesFromCSV()
    print("Bye!")

if __name__ == "__main__":
    main()

main()
what do you expect with lines 87 and 89?
(Jan-29-2018, 09:46 AM)buran Wrote: [ -> ]what do you expect with lines 87 and 89?
LOL

I got to be more careful with copying and pasting!