python - 根据 self.questions 列表为答案创建 if, else 语句
问题描述
如何从self.questions
列表中创建这些 if 和 else 语句?我现在要做的是,当程序运行时,有三个选项框,每个选项框都有答案。
self.questions
我希望用户选择其中一个答案,并根据与所问问题对应的列表中的答案查看它们是否正确。
def __init__(self):
self.newWindow = self.new_window('QUESTION')
self.window = self.newWindow
#This is a counter for the questions
self.question_counter = 0
#This is the questions, there is a list of questions
self.questions = ["Question One: What is the capital of France?", "Question Two: What martial arts movie was first produced by Hollywood?",
"Question Three: What year did World War II start?", "Question Four: What year did Abraham Licoln get assassinated?"]
#This is the answers, there is a list of three possible answers for each question
self.answers = [["Madrid", "London", "Paris"],
["The Karate Kid", "Enter the Dragon", "Ip Man"],
["1914", "1939", "1940"],
["1865", "1912", "1688"]]
self.label = Label(self.newWindow,
text=self.questions[self.question_counter], font = ("Arial", "24"), pady=30)
self.answer1button = Button(self.window, text=self.answers[self.question_counter][0])
self.answer2button = Button(self.window, text=self.answers[self.question_counter][1])
self.answer3button = Button(self.window, text=self.answers[self.question_counter][2])
解决方案
您可以在类中创建三个方法,每个方法对应一个答案/按钮。在函数内部检查选择的答案是否正确,如果正确,则在问题计数器中添加一个并显示下一个问题。然后让每个按钮执行与其相关的功能。
推荐阅读
- php - 为什么我可以在这个 HTML 表中打印这个索引数组的值?
- haskell - splitOn 的内存占用?
- java - Firebase Android:获取存储在用户 ID 中的数据,然后获取随机 Push().getKey() 值
- java - 重新声明/重新定义 Java 变量循环
- r - 如何计算 R 曲线下面积的 Sobol 灵敏度?
- reactjs - 在 Jest 中测试无状态子组件中的父方法
- object - 将多个对象添加到与所选字符串匹配的 ArrayList
- youtube-api - YouTube 报告 API 报告均为空白
- javascript - 单击按钮后,Ionic -angular 无法更新视图
- spring-boot - 过滤器是否适合在 Spring 中根据标头值处理服务调用?