0

I am using Python 3.5 with the openpyxl module. I have a small excel spreadsheet where it has 5 columns, each has 5 movies in a specific genre. My Python program is designed to, based on user input, display the movies of a certain genre, add a movie to the spreadsheet, delete a movie, or just exit. Now, I have got it to display the movies in a category the user chooses, however, I cannot get it to delete correctly.

It will show the list of movies in the genre and regardless of what number the user enters, it automatically chooses option #5. I do not know how to get it to correctly match.

To make matters worse, when given the option to delete (by entering 'Y' or 'N'), sometimes it still deletes anyways.

And, even though the excel workbook is supposed to "save" - once you restart the program, the deleted file shows up again.

I've been looking at this code for hours and I don't know what to change. Can you guys please take a look and give me some suggestions?

I know you can't access the spreadsheet, but here isn image of the spreadsheet layout:

Movie Excel sheet

and here is the code from the beginning :

import openpyxl
wb = openpyxl.load_workbook("Movies.xlsx")
sheet = wb.active
sheetname = wb.get_sheet_by_name("Movies")

thrillers = list(sheet['A2' : 'A6'])
comedies = list(sheet['B2' : 'B6'])
action = list(sheet['C2' : 'C6'])
dramas = list(sheet['D2' : 'D6'])
family = list(sheet['E2' : 'E6'])

def greeting():
    print("\tWELCOME TO YOUR MOVIE DATABASE!\n")
    print("Please select an option from the menu:\n")

def menu_selection():
    print("\n")
    print("\t ** MENU **")
    print("1. Search the movie database\n" \
          "2. Add a movie to the database\n" \
          "3. Delete a movie from the database\n" \
          "4. Exit the program\n")

And here is the code from the function that displays the movies in the database it, depending on the genre entered by user:

def movie_selector():
print("1. Thriller\n" \
      "2. Comedy\n" \
      "3. Action\n" \
      "4. Drama\n" \
      "5. Family\n" \
      "6. Exit to main menu")

print("\n")
selection = int(input("Enter your selection: "))
print("\n")    

if selection == 1:
    print("\t ** " + sheet['A1'].value + " **")
    for m in thrillers:
        print(m[0].value)
elif selection == 2:
    print("\t ** " + sheet['B1'].value + " **")
    for m in comedies:
        print(m[0].value)
elif selection == 3:
    print("\t ** " + sheet['C1'].value + " **")
    for m in action:
        print(m[0].value)
elif selection == 4:
    print("\t ** " + sheet['D1'].value + " **")
    for m in dramas:
        print(m[0].value)
elif selection == 5:
    print("\t ** " + sheet['E1'].value + " **")
    for m in family:
        print(m[0].value)
elif selection == 6:
    menu_selection()
else:
    print("Invalid selection. Try again.")
    movie_selector()

And here is the function to delete a movie, and nothing works right:

def delete_movies():
print("\t ** CATEGORIES **\n")
print("1. Thriller\n" \
      "2. Comedy\n" \
      "3. Action\n" \
      "4. Drama\n" \
      "5. Family\n" \
      "6. Exit to main menu")

selection = int(input("\nSelect the category of the movie you want to delete: "))    
n = 0

print("\n")

if selection == 1:        
    print("\t ** " + sheet['A1'].value + " **")
    for m in thrillers:
        n += 1            
        print(str(n) + '. ' + m[0].value)
elif selection == 2:
    print("\t ** " + sheet['B1'].value + " **")
    for m in comedies:
        n += 1            
        print(str(n) + '. ' + m[0].value)
elif selection == 3:
    print("\t ** " + sheet['C1'].value + " **")
    for m in action:
        n += 1            
        print(str(n) + '. ' + m[0].value)
elif selection == 4:
    print("\t ** " + sheet['D1'].value + " **")
    for m in dramas:
        n += 1            
        print(str(n) + '. ' + m[0].value)
elif selection == 5:
    print("\t ** " + sheet['E1'].value + " **")
    for m in family:
        n += 1            
        print(str(n) + '. ' + m[0].value)
elif selection == 6:
    menu_selection()
else:
    print("Invalid selection. Try again.")
    movie_selector()

delete_num = int(input("\nEnter the number of the movie to delete: "))    

if delete_num == 1:
    confirm = input("Delete " + m[0].value + " (Y or N)?  ")    # it outputs #5 value no matter what

    if confirm == 'y' or 'Y':
        print("Ok, deleted.")
        m[0].value = ""
        wb.save("Movies.xlsx")            

    elif confirm == 'n' or 'N':
        print("Not deleted")

elif delete_num == 2:
    confirm = input("Delete " + m[0].value + " (Y or N)?  ")


    if confirm == 'y' or 'Y':
        print("Ok, deleted.")
        m[0].value = ""
        wb.save("Movies.xlsx")

    elif confirm == 'n' or 'N':
        print("Not deleted.")

elif delete_num == 3:
    confirm = input("Delete " + m[0].value + " (Y or N)?  ")

    if confirm == 'y' or 'Y':
        print("Ok, deleted.")
        m[0].value = ""
        wb.save("Movies.xlsx")

    elif confirm == 'n' or 'N':
        print("Not deleted.")

elif delete_num == 4:
    confirm = input("Delete " + m[0].value + " (Y or N)?  ")

    if confirm == 'y' or 'Y':
        print("Ok, deleted.")
        m[0].value = ""
        wb.save("Movies.xlsx")

    elif confirm == 'n' or 'N':
        print("Not deleted.")

elif delete_num == 5:
    confirm = input("Delete " + m[0].value + " (Y or N)?  ")

    if confirm == 'y' or 'Y':
        print("Ok, deleted.")
        m[0].value = ""
        wb.save("Movies.xlsx")

    elif confirm == 'n' or 'N':
        print("Not deleted.")

else:
    print("Invalid selection. Try again.")

I hope someone can tell me what is going wrong. I will greatly appreciate it.

4
  • It seems that you did not manipulate the excel object directly, you were just making some changes to the list object when you executed the delete function. Commented Nov 30, 2016 at 7:37
  • Thank you for your response. How would I manipulate it directly? Commented Nov 30, 2016 at 7:46
  • my suggestion would be using xlrd module, because it's easy to learn, and the operations provided can be easily performed. Commented Nov 30, 2016 at 7:49
  • xlrd cannot save existing files so won't be any use here. Commented Nov 30, 2016 at 8:28

2 Answers 2

2

In your delete you are modifying M:

m[0].value = ""

M however is just the value inside your program. You need to modify the Cell of the Worksheet:

sheetname['A2'] = ""

You can build up the cell string ('A2', like 'A' + str(i)) to delete the right cell

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! This is working!! I'm sure I'll run into some more questions at some point, but this gets me over the current big hump I've been stuck on.
0

Whit this if delete_num == 1: confirm = input("Delete " + m[0].value + " (Y or N)? ") # it outputs #5 value no matter what You are trying to access a variable that you have declared in an IF statement. You should declare it right after the start of your function like this def delete_movies(): m = "" print("\t ** CATEGORIES **\n") print("1. Thriller\n" "2. Comedy\n" "3. Action\n" "4. Drama\n" "5. Family\n" "6. Exit to main menu")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.