'Application prints duplicate element Python
I want the write an application that can print out all the questions from a list. But not sure what is wrong with my code, the list does not print out in incremental order. Instead it goes from 0,0,1,0,1,2. I don't understand why it has to reprint the previous element. I expect the list to print out like 0,1,2,3 ... I know there is another alternative to get the result I want, but I want to understand what I did wrong. I am a newbie, any help is appreciated!
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()
data.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"}
]
main.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()
Solution 1:[1]
It works properly:
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()
Solution 2:[2]
As mentioned in the comments, it would be useful to know how you're calling the class.
Why are you using current_question.text? What are you trying to pass when creating the object?
Anyway, keeping your code basically as it is (just drop the -1 in the while condition, and pass a list of strings as questions), you could do something like:
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()
Or you could simplify it a little:
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vasily |
| Solution 2 |
