应用程序打印重复的元素Python



我希望编写一个应用程序,可以打印出列表中的所有问题。但不确定我的代码出了什么问题,列表不会按递增顺序打印出来。相反,它从0,0,1,0,1,2开始。我不明白为什么它必须重印前一个元素。我希望列表能像0,1,2,3一样打印出来。。。我知道还有另一种选择可以得到我想要的结果,但我想明白我做错了什么。我是一个新手,任何帮助都将不胜感激!

question_model.py

class Question:
def __init__(self, text, answer):
self.text = text
self.answer = answer

quiz_brain.py

class QuizBrain:
def __init__(self,questions_list):
self.question_number = 0
self.questions_list = questions_list
self.number_of_question = len(questions_list)

def next_question(self):
current_question = self.questions_list[self.question_number]
input(f"{self.question_number}:{current_question.text} (True/False): ")
self.question_number += 1

def still_has_questions(self):
while self.question_number < self.number_of_question-1:
self.next_question()

数据.py

question_data = [
{"text": "A slug's blood is green.", "answer": "True"},
{"text": "The loudest animal is the African Elephant.", "answer": "False"},
{"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"},
{"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"},
{"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"},
{"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"},
{"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"},
{"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"},
{"text": "Google was originally called 'Backrub'.", "answer": "True"},
{"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"},
{"text": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"},
{"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}
]

主.py

from question_model import Question
from data import question_data
from quiz_brain import QuizBrain


question_bank = []
for i in question_data:
question_text = i["text"]
question_answer = i["text"]
new_question = Question(question_text,question_answer)
question_bank.append(new_question)
quiz = QuizBrain(question_bank) #create the new quiz object
quiz.still_has_questions()

它工作正常:

class QuizBrain:
def __init__(self, questions_list):
self.question_number = 0
self.questions_list = questions_list
self.number_of_question = len(questions_list)
def next_question(self):
current_question = self.questions_list[self.question_number]
input(f"{self.question_number}:{current_question} (True/False): ")
self.question_number += 1
def still_has_questions(self):
while self.question_number <= self.number_of_question - 1:
self.next_question()

quest = QuizBrain(['first question', 'second question', 'third question'])
quest.still_has_questions()

如注释中所述,了解如何调用该类将非常有用
为什么使用current_question.text?创建对象时,您试图传递什么?无论如何,保持代码的基本原样(只需在while条件中去掉-1,并将字符串列表作为问题传递(,您可以执行以下操作:

class QuizBrain:
def __init__(self, questions_list):
self.question_number = 0
self.questions_list = questions_list
self.number_of_question = len(questions_list)
def next_question(self):
current_question = self.questions_list[self.question_number]
input(f"{self.question_number}:{current_question} (True/False): ")
self.question_number += 1
def still_has_questions(self):
while self.question_number < self.number_of_question:
self.next_question()

quest = QuizBrain(['question 1', 'question 2', 'question 3'])
quest.still_has_questions()

或者你可以稍微简化一下:

class QuizBrain:
def __init__(self, questions_list):
self.questions_list = questions_list

def ask_questions_until_end(self):
for index, question in enumerate(self.questions_list):
input(f"{index}: {question} (True/False): ")

quiz = QuizBrain(['question 1', 'question 2', 'question 3'])
quiz.ask_questions_until_end()

最新更新