Reputation: 23
I am doing a code for school which show asks students science questions, it works but is case sensitive meaning if the entry in the dictionary is lower case and they write in upper case it will say wrong answer, i have tried adding .upper, but it did not work
this is the dictionaries part
def next_problem(self):
"""Creates a problem, stores the correct answer"""
questions = {1:{'What star shines in the day and gives us light? ':'sun', 'What is the young one of a cow called?':'calve',
'What do birds use to fly? ':'wings', 'what gas do humans use to breath':'oxyen','what insect has 8 legs':'spider','What do Chickens lay?':'eggs','What do Cow produce?':'milk'},
2:{"What do Bee's collect from flowers?":'pollen', 'Is a Tomato conidered a fruit or a vegetable?':'fruit', 'How many bones do humans have?':'206',
'What body part allows us to smell?':'nose', 'How many planets are in the solar system?':'8', "Pluto is a planet, ture or flase":"false","What season has the most rain?":'winter',
'What measuring system do we use in New Zealand?':'metric'},
3:{'Which organ covers the entire body and protects it?':'skin', 'If one boils water it will convert into?':'steam', 'When you push something, you apply a?':'force',
'Animals that eat both plants and meat, called?':'omnivores', 'Which is the closest planet to the sun?':'mercury', 'Where is lightning formed during thunderstorms?'
:'clouds','Atoms with a positive charge is called?':'proton'}}
this is the check answer part
def check_answer(self):
try:
ans = self.AnswerEntry.get()
if ans == self.answer:
self.feedback.configure(text = "Correct!")
self.score += 1
self.AnswerEntry.delete(0, END)
self.AnswerEntry.focus()
self.next_problem()
else:
self.feedback.configure(text = "Wring")
self.AnswerEntry.delete(0, END)
self.AnswerEntry.focus()
self.next_problem()
except ValueError:
self.feedback.configure(text = "wrong answer")
self.AnswerEntry.delete(0, END)
self.AnswerEntry.focus()
if self.score <= 5:
self.report_score.configure(text = str(self.score))
Upvotes: 1
Views: 41
Reputation: 9494
You can compare both answers after performing lower()/upper()
on both:
if ans.lower() == self.answer.lower():
# your logic
Upvotes: 2