我的Python代码有逻辑错误,但不知道如何解决

我正在使用Python 3.5与openpyxl模块。 我有一个小的Excel电子表格,它有5列,每个有5个特定types的电影。 我的Python程序旨在基于用户input,显示某种types的电影,向电子表格添加电影,删除电影或退出。 现在,我已经得到它在用户select的类别中显示电影,但是,我无法正确地删除它。

它将显示stream派中的电影列表,不pipe用户input什么号码,都会自动select选项#5。 我不知道如何让它正确匹配。

更糟糕的是,当给出删除选项时(通过input“Y”或“N”),有时仍然会删除。

而且,即使Excel工作簿应该“保存” – 一旦你重新启动程序,被删除的文件再次出现。

我一直在看这个代码几个小时,我不知道要改变什么。 你们可以请看看,给我一些build议吗?

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

电影Excel工作表

这里是从一开始的代码:

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

下面是从数据库中显示电影的函数代码,具体取决于用户input的types:

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

这里是删除电影的function,没有任何工作正确的:

 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'] = "" 

你可以build立单元格string('A2',比如'A'+ str(i))来删除右边的单元格

if delete_num == 1: confirm = input("Delete " + m[0].value + " (Y or N)? ") # it outputs #5 value no matter what你想要访问一个variables已经在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")