首页 > 解决方案 > 根据 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])

标签: pythontkinter

解决方案


您可以在类中创建三个方法,每个方法对应一个答案/按钮。在函数内部检查选择的答案是否正确,如果正确,则在问题计数器中添加一个并显示下一个问题。然后让每个按钮执行与其相关的功能。


推荐阅读