如何让python gui字典,在输入答案时不区分大小写?

问题描述 投票:1回答:1

我正在为学校做一个代码,显示问学生科学问题,它的工作,但大小写敏感的意思,如果在字典中的条目是小写,他们写在大写,它会说错误的答案,我已经尝试添加.upper,但它没有工作。

这是字典的一部分

    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'}} 

这是检查答案部分

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))
python python-3.x tkinter case-insensitive
1个回答
2
投票

你可以比较两个答案后执行 lower()/upper() 在两个。

if ans.lower() == self.answer.lower():
    # your logic
© www.soinside.com 2019 - 2024. All rights reserved.