我的 Python 代码有逻辑错误,但不确定具体如何修复

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

我正在使用 Python 3.5 和 openpyxl 模块。 我有一个小型 excel 电子表格,其中有 5 列,每列有 5 部特定类型的电影。 我的 Python 程序旨在根据用户输入显示特定类型的电影、将电影添加到电子表格、删除电影或直接退出。 现在,我已经可以在用户选择的类别中显示电影,但是,我无法正确删除它。

它将显示该类型的电影列表,无论用户输入多少编号,它都会自动选择选项 #5。我不知道如何让它正确匹配。

更糟糕的是,当提供删除选项时(通过输入 'Y' 或 'N'),有时它仍然会删除。

而且,即使 excel 工作簿本应 "save" - 一旦您重新启动程序,已删除的文件又会出现。

我已经查看这段代码好几个小时了,但我不知道要更改什么。 能不能请大家看看并给我一些建议?

我知道您无法访问电子表格,但这是电子表格布局的图片:

这是开头的代码:

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")

这里是根据用户输入的类型显示数据库中电影的函数代码:

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()

这里是删除电影的功能,但是什么都不对:

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.")

我希望有人能告诉我哪里出了问题。我将不胜感激。

在你的删除中你正在修改 M:

m[0].value = ""
然而,

M 只是您程序中的值。您需要修改工作表的单元格:

sheetname['A2'] = ""

您可以构建单元格字符串('A2',如 'A' + str(i))以删除正确的单元格

这个if delete_num == 1: confirm = input("Delete " + m[0].value + " (Y or N)? ") # it outputs #5 value no matter what 您正在尝试访问您在 IF 语句中声明的变量。你应该像这样在函数开始后立即声明它 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")