首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使python gui字典在输入答案时不区分大小写。

如何使python gui字典在输入答案时不区分大小写。
EN

Stack Overflow用户
提问于 2020-05-06 14:26:34
回答 1查看 35关注 0票数 1

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

这是字典部分

代码语言:javascript
运行
复制
    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'}} 

这是检查答案部分。

代码语言:javascript
运行
复制
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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-06 14:31:58

在对两个答案执行lower()/upper()后,您可以比较这两个答案:

代码语言:javascript
运行
复制
if ans.lower() == self.answer.lower():
    # your logic
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61628731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档