获取用户输入并检查其是否存在于列表中



我正在尝试检查一个名为categoriesList的列表中是否存在作为字符串的用户输入,该列表附加了名为cateCategories.txt的文本文件中的类别。如果用户输入的类别随后存在于categories list中,我的代码应该能够打印出"类别存在";,否则";类别不存在"。

这是代码:

categoriesList = []
with open("categories.txt", "r") as OpenCategories:
for category in (OpenCategories):
categoriesList.append(category)
while True:
inputCategories = input("Please enter a category:")
if inputCategories in categoriesList:
print("Category exists")
break
else:
print("Category doesn't exist")
break

当我运行此代码时,它总是输出Category不存在,即使我输入的类别实际上存在于categoriesList中。我将如何在代码中解决此问题?此外,我希望能够从用户获得用于输入类别的一个输入;请输入一个类别";我只想让代码只出现一次。

此外,如果我能知道如何在tkinter中完成上述所有操作的代码,我将不胜感激,因为我需要在GUI中完成上述操作。我认为你需要有标签,并允许用户在屏幕上的框中输入一个类别。

我试图制作代码,在tk屏幕上获得输入后,尝试检查用户输入是否存在于列表中,因为这不足以让我在python控制台中进行检查,而且做得不好,所以下面是代码:

import tkinter as tk
from tkinter import ttk
window=tk.Tk()
canvas1 = tk.Canvas(window, width = 400, height = 300)
canvas1.pack()
label1 = Label(window, text="Please enter a category:")
label1.pack()
entry = Entry(window, width=50)
entry.pack()
def for_button():
checkUserInput = entry.get()
button = Button(window, text="Check", command=for_button)
button.pack()
for i in categoriesList:
if button in categoriesList:
categoryExist = Label(window, text="Category exists")
categoryExist.pack()
else:
categoryNotExist= Label(window, text="Category doesn't exist")
categoryNotExist.pack()
window.mainloop() 

它使用了来自给定帖子中代码编译器的列表类别list,我正试图让用户在tk屏幕上的文本框中输入一个类别,然后单击";"检查";按钮之后但在用户可以给出输入之前;类别不存在";出现了很多次,这是我不希望代码执行的操作。

当您从文本文件中读取时,您也会读取特殊字符,如'n',因此必须将其删除。这可以按照下面的代码所示进行。

我还稍微调整了你的循环布局,因为你不想在for循环中有while循环——否则你会在添加所有类别之前搜索列表。

categoriesList = []
with open("categories.txt", "r") as OpenCategories:
for category in (OpenCategories):
categoriesList.append(category.strip()) #remove newline, tabs etc

inputCategories = input("Please enter a category:")
if inputCategories in categoriesList:
print("Category exists")
else:
print("Category doesn't exist")

您可能还想从类别列表中删除任何空格' '。这可以通过更换来实现

category.strip()

带有

category.strip().replace(' ','')

您可能还希望将输入的变量和类别都设置为小写,以便进行比较。这可以使用字符串上的lower函数来完成。

例如inputCategories = inputCategories.lower()

while循环不应该在for循环内,while循环根本不必要:

with open("categories.txt", "r") as OpenCategories:
categoriesList = OpenCategories.read().splitlines()
# don't need the while loop
inputCategories = input("Please enter a category:")
if inputCategories in categoriesList:
print("Category exists")
else:
print("Category doesn't exist")

对于使用GUI更新的代码,您需要将检查放在for_button()函数中:

import tkinter as tk
# load category list from file
with open("categories.txt") as OpenCategories:
categoriesList = OpenCategories.read().splitlines()
window = tk.Tk()
label1 = tk.Label(window, text="Please enter a category:")
label1.pack()
# user input category
entry = tk.Entry(window, width=50)
entry.pack()
def for_button():
checkUserInput = entry.get()
if checkUserInput in categoriesList:
result.config(text="Category exists", fg="green")
else:
result.config(text="Category doesn't exist", fg="red")
button = tk.Button(window, text="Check", command=for_button)
button.pack()
# label for check result
result = tk.Label(window)
result.pack()
window.mainloop()

最新更新