You just need the value of the dict
if podane_znaczenie == slownik[key]:
You could also write it as
for key, value in slownik.items():
print ('Do you know the meaning of this character', key, '?')
podane_znaczenie = input ('Meaning: ')
if podane_znaczenie == value:
print ('Yes!')
continue
PS: continue is not needed here
I've recently began learning Python, and I'm trying to write a simple Q&A program. I want the user to be able to insert input and have it be compared to keys in a dictionary, and whichever key that has the most words in common with the question will be activated, and the answer will be printed. Here is my current code: 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()What could I add to compare the words in common with the user input with the keys in the dictionary? Thanks!
python - How to compare user input with the keys of a dictionary? - Stack Overflow
Python - How to compare user input to dictionary keys? - Stack Overflow
python - Is there a better way to compare dictionary values - Stack Overflow
comparing a user input to a dictionary in python - Stack Overflow
Videos
You just need the value of the dict
if podane_znaczenie == slownik[key]:
You could also write it as
for key, value in slownik.items():
print ('Do you know the meaning of this character', key, '?')
podane_znaczenie = input ('Meaning: ')
if podane_znaczenie == value:
print ('Yes!')
continue
PS: continue is not needed here
In Python, key-value pairs can be treated as tuples. This lets you iterate through a dictionary in a very handy way:
for key, value in my_dict:
print(key)
print(value)
Using this method, we can fill in the missing space in your code like this:
for key, value in slownik:
print ('Do you know the meaning of this character', key, '?')
podane_znaczenie = input ('Meaning: ')
if podane_znaczenie == value:
print ('Yes!')
continue
Furthermore, while you didn't ask for this exactly, the continue statement is not needed in this code snippet. Because there is no more code after it, the loop will continue on its own :)
If the true intent of the question is the comparison between dicts (rather than printing differences), the answer is
dict1 == dict2
This has been mentioned before, but I felt it was slightly drowning in other bits of information. It might appear superficial, but the value comparison of dicts has actually powerful semantics. It covers
- number of keys (if they don't match, the dicts are not equal)
- names of keys (if they don't match, they're not equal)
- value of each key (they have to be '==', too)
The last point again appears trivial, but is acutally interesting as it means that all of this applies recursively to nested dicts as well. E.g.
m1 = {'f':True}
m2 = {'f':True}
m3 = {'a':1, 2:2, 3:m1}
m4 = {'a':1, 2:2, 3:m2}
m3 == m4 # True
Similar semantics exist for the comparison of lists. All of this makes it a no-brainer to e.g. compare deep Json structures, alone with a simple "==".
If the dicts have identical sets of keys and you need all those prints for any value difference, there isn't much you can do; maybe something like:
diffkeys = [k for k in dict1 if dict1[k] != dict2[k]]
for k in diffkeys:
print k, ':', dict1[k], '->', dict2[k]
pretty much equivalent to what you have, but you might get nicer presentation for example by sorting diffkeys before you loop on it.
Here is the scenario:
dict1 = { A : 1 , B : 3 , C: 0 }
dict2 = {C : 1, D : 2 , A : 1, B : 3 }
dict3 = {E : 1, B : 3, A : 1, C : 0}
Assuming dict1 is the baseline that subsequent dictionaries (dict2, dict3) need to be compared against, I am thinking about codes that check if dict 2 >= dict1 or dict 3 >= dict1:
-
I understand dictionary is unordered, so does that mean that the dictionary can be compared without sort method?
-
does extra elements in dict2 and dict3 affect the comparison operation, should my code factor extra elements?
-
i think the best way would be something similar to cmp() in Python2, is any function similar to cmp() for dictionary? I don't intend to import any outside library, but okay with builtin modules.
I would like to some pointers to solve the question. Thank you for the help in advance!