Home > Blockchain >  I'm having a problem with lists in my basic quiz software
I'm having a problem with lists in my basic quiz software

Time:03-11

I am running the code block written below:

class Question:
    
    def __init__(self,text,choices,answer):
        self.text = text
        self.choices = choices
        self.answer = answer
        
    def checkAnswer(self, answer):
        return self.answer == answer
 class Quiz:
    
    def __init__(self, questions):
        self.questions = questions
        self.score = 0
        self.questionsIndex = 0
        
    def getQuestion(self):
        return self.questions[self.questionsIndex]
    
    def displayQuestion(self):
        question = self.getQuestion()
        print(f"Question: {self.questionsIndex  1}: {question.text}")   
        for q in question.choices:
            print("-"  q)
        answer = input("Your Answer:  ")
        self.guess(answer)
        self.loadQuestion()
        
    def guess(self, answer):
        question = self.getQuestion()
        if question.checkAnswer(answer):
            self.score  = 1
        self.questionsIndex  = 1
        self.displayQuestion()
        
    def loadQuestion(self):
        if len(self.questions) == self.questionsIndex:
            self.showScore()
        else:
            self.displayProgress()
            self.displayQuestion()
            
    def showScore(self):
        print("Score: ", self.score)
        
    def displayProgress(self):
        totalQuestion = len(self.questions)
        questionNumber = self.questionsIndex   1
        if questionNumber > totalQuestion:
            print("Quiz Finished")
        else:
            print(f"*************************Question {questionNumber} of {totalQuestion}***********************************")
           

q1 = Question("Which programming language is the most profitable?["C#","Python","Java","HTML"],"Python")
q2 = Question("Which is the easiest programming language?", ["C#","Python","Java","HTML"],"Python")
q3 = Question("What is the most popular programming language?", ["C#","Python","Java","HTML"],"Python")
questions = [q1,q2,q3]
quiz = Quiz(questions)
quiz.loadQuestion()

And I am facing the following problem:

runfile('C:/Users/Onur/Desktop/Artificial Intelligence A-Z/sorularclass.py', wdir='C:/Users/Onur/Desktop/Artificial Intelligence A-Z')
*************************Question 1 of 3***********************************
Question: 1: Which programming language is the most profitable?
-C#
-Python
-Java
-HTML
 Your Answer:  a
Question: 2: Which is the easiest programming language?
-C#
-Python
-Java
-HTML
Your Answer:  a
Question: 3: What is the most popular programming language?
-C#
-Python
-Java
-HTML
Your Answer:  a
Traceback (most recent call last):
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 63, in <module>
    quiz.loadQuestion()
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 44, in loadQuestion
    self.displayQuestion()
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 29, in displayQuestion
    self.guess(answer)
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 37, in guess
    self.displayQuestion()
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 29, in displayQuestion
    self.guess(answer)
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 37, in guess
    self.displayQuestion()
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 29, in displayQuestion
    self.guess(answer)
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 37, in guess
    self.displayQuestion()
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 24, in displayQuestion
    question = self.getQuestion()
File "C:\Users\Onur\Desktop\Artificial Intelligence A-Z\sorularclass.py", line 21, in getQuestion
    return self.questions[self.questionsIndex]

IndexError: list index out of range

Can you tell me the reason for this? Why is there a problem with lists? I'm adding this because stackoverflow wants me to add more details: I tried to build a quiz using basic class methods in this software, but I ran into a problem.

CodePudding user response:

In the displayQuestion method you call the guess method. In the guess method you increase the questionsIndex value, and call displayQuestion method again.

This process repeats and repeats infinitely until the questionIndex goes out of range. It seems that you need to remove calling the displayQuestion method from the guess method.

  • Related