私は最近Pythonを学び始めました。単純なQ & Aプログラムを書こうとしています。私は、ユーザーが入力を挿入し、それを辞書のキーと比較させ、質問と共通の単語が最も多いキーがアクティブになり、答えが印刷されるようにしたい。現在のコードは次のとおりです。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()'は、ユーザーが入力したテキストを返しますか? – Bhargav
それは正しいでしょう。 – rpeopler
電話帳の項目に入力した数字を比較したときに、CodeAcademyの基本的なJavaScriptを学んだときに似たようなことを覚えています(どういう意味ですか、P:何かを覚えていません)。私がしたいことは、それにかなり似ています。 – rpeopler