my problem is the programme won’t state the answer as correct only if you put the answer alone, but i want to make that you can actually put a phrase in the entry widget and if the answer is in that phrase it would say correct, i made this work without tkinter but i canno’t make it work in tkinter here is my code, could you please help, thanks.
the code
JavaScript
x
65
65
1
import tkinter as tk
2
from time import sleep
3
4
win = tk.Tk()
5
6
win.title("Learning game")
7
8
class Question:
9
def __init__(self,prompt,answer):
10
self.answer = answer
11
self.prompt = prompt
12
13
score = 0
14
15
16
17
def ask():
18
global questions
19
global useranwser
20
global score
21
if questions:
22
#Check both the possible answers
23
if useranwser.get() in questions[0].answer:
24
print("Correct")
25
score += 1
26
else:
27
print("Incorrect")
28
questions.pop(0)
29
if questions:
30
s.set(questions[0].prompt)
31
else:
32
print('Done')
33
print("Your score is : ",score)
34
quit() #optional
35
useranwser.set('')
36
37
38
question_prompts = [
39
"When did Tunisia gain independence? n 1956 , 1984 , 1965 n", "What is the captial of tunis ? n Tunis, Gafsa, Nabuel n",
40
"Who won 2018 football world cup ? n Italy, France, England n","how long is the eiffel tower ?n 324m, 354m, 412m n",
41
"What is the heaviest metal n Iron, Copper, Uraniam n "
42
]
43
44
questions = [
45
Question(question_prompts[4], "uraniam"),
46
Question(question_prompts[0], "1956"),
47
Question(question_prompts[1], "tunis"),
48
Question(question_prompts[2], "france"),
49
Question(question_prompts[3], "324m")
50
]
51
52
s = tk.StringVar()
53
useranwser = tk.StringVar()
54
q = tk.Label(win,textvariable = s)
55
q.grid(row = 0, column = 0)
56
u_answer = tk.Entry(win, textvariable = useranwser)
57
u_answer.grid(row = 1, column = 0)
58
b = tk.Button(win, text ="Submit", command = ask)
59
b.grid(row = 2, column =0 )
60
s.set(questions[0].prompt)#Set the initial question
61
62
63
64
win.mainloop()
65
Advertisement
Answer
I had to flip this if statment from this :
JavaScript
1
4
1
if useranwser.get() in questions[0].answer:
2
print("Correct")
3
score += 1
4
to this:
JavaScript
1
4
1
if questions[0].answer in useranwser.get():
2
print("Correct")
3
score += 1
4
Thanks to @justin ezequiel for the solution and @random davis for a debugging tip