繁体   English   中英

Python-如何将用户输入与字典键进行比较?

[英]Python - How to compare user input to dictionary keys?

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

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()的结果保存在变量中,以便将其与字典中的每个键进行比较。

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语句之前的变量中,如示例所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM