Python Forum
Why am I being prompted to exit the program twice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why am I being prompted to exit the program twice?
#1
For some reason, after exiting the while true loop with the exit choice, my program makes me exit twice (see the image i attached).
   
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()
Reply
#2
what do you expect with lines 87 and 89?
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 579 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  python difference between sys.exit and exit() mg24 1 1,847 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  force a program to exit ? Armandito 3 2,542 May-12-2022, 04:03 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020