Jess Jess - 9 days ago 5
Python Question

My Python code has logic errors, but not sure exactly how to fix

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.

Answer

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