【问题标题】:Python - How to compare user input to dictionary keys?Python - 如何将用户输入与字典键进行比较?
【发布时间】:2016-03-20 18:07:21
【问题描述】:

我最近开始学习 Python,正在尝试编写一个简单的问答程序。我希望用户能够插入输入并将其与字典中的键进行比较,并且将激活与问题有最多共同词的键,并打印答案。这是我当前的代码:

from tkinter import * 
from tkinter import ttk 
from tkinter import messagebox
root = Tk()
root.geometry('{}x{}'.format(666, 666))
var = StringVar()
vara = StringVar()

resp = {'What programming language was this written in?': 'This program was written using Python 3.5. Python is a widely used high-level, general-purpose, dynamic programming language. Its syntax enables programmers to write code in fewer lines than more complex languages like Java and C++.', 'Who invented computer programming?': 'Charles Babbage is universally accepted as the father of computer programming due to his creation of the analytical engine. While computers were not created until a century beyond his invention, the analytical engine used an identical concept for input/output commands.', 'What coding language was used to create Windows OS?': 'Windows 7, 8, 8.1 and 10 operate on C++ and C# almost exclusively. Because these are lower-level languages, the programmer has greater control over the computer itself, enabling them to do many amazing things.'}


label = Message( root, textvariable=var,      relief=RAISED)
labelfont = ('times', 20, 'bold')

def callback():

    parabel = Tk()
    parabel.minsize(600, 400)
    parabel.maxsize(600,400)
    parabel.title("BonneyBot")

    pLabel = Label(parabel, text = "Welcome to     BonneyBot.").pack(side = TOP)
    pLabel1 = Label(parabel, text = "Ask a question about programming!").pack()
    pEntry1 = ttk.Entry(parabel, textvariable = vara)
    pEntry1.pack(side='top')

    def response():
        if pEntry1.get() in resp.keys():
            messagebox.showinfo('file',  resp[pEntry1.get()])
        else:
            messagebox.showwarning('error', 'I did not understand the question. Please ask again.')



    ttk.Button(parabel, text = "ASK AWAY!",     command=response).pack()




widget = Label(root, text="Welcome to my graduation project!\n This is a simple Q&A program created\n by Devin intended to assist individuals\n curious about computer programming.\n Click start to begin!")
widget.config(bg='lightblue', fg='red')
widget.config(font=labelfont)
widget.config(height=3, width=20)
widget.pack(expand=YES, fill=BOTH)
var.set("Let's get started!")


MyButton1 = Button(root, text="START", command=callback)
MyButton1.pack()



label.pack()
root.mainloop()

我可以添加什么来比较用户输入的共同单词和字典中的键?我很确定我会使用 for 循环将常用词与每个键进行比较以打印值,但我不确定如何执行此操作。任何帮助,将不胜感激!谢谢!

【问题讨论】:

  • 为了确定,pEntry1.get() 返回用户输入的文本?
  • 那是正确的。
  • 我记得当我在 CodeAcademy 上学习基本 JavaScript 时做过类似的事情(多么菜鸟,对吗?:P),当我将输入的数字与电话簿中的条目进行比较时,但我不记得了这该怎么做。我想做的和那个差不多。
  • 那么response 函数中的 if 语句已经在检查用户输入的任何部分是否存在于字典 resp 的任何键中。这不是你想要的吗?
  • 本质上,但是响应功能只有在用户输入逐字插入键时才会激活。但是,我希望将用户输入与每个键进行比较,以便用户无需键入整个键即可获得响应......例如,如果用户键入“谁发明”,那么我希望为“打印相应的值”谁发明了计算机编程。”这有帮助吗?

标签: python dictionary input tkinter


【解决方案1】:

您需要做的是将pEntry1.get() 的结果保存在一个变量中,以便您可以将其与字典中存在的每个键进行比较。

user_input = pEntry1.get()

for question in resp.keys():
    if user_input in question:
        messagebox.showinfo('file', resp[question])
....

这将检查用户输入的任何部分是否在 resp 字典中的任何键中。

PS:您应该将 pep8 作为样式指南并使用更多变量。例如pEntry1.get() 被调用了几次,它可以存储在if 语句之前的变量中,如示例所示。

【讨论】:

  • 这肯定有帮助,但现在由于某种原因,即使 if 语句的条件满足,else 语句也会被激活。我将 for 循环插入到 if/then 语句中。
猜你喜欢
  • 1970-01-01
  • 2018-05-17
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多