我的基本测验软件中的列表有问题
I'm having a problem with lists in my basic quiz software
我是运行下面写的代码块:
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()
我面临以下问题:
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
你能告诉我这是什么原因吗?为什么列表有问题?我添加这个是因为 Whosebug 希望我添加更多细节:我试图在这个软件中使用基本 class 方法构建一个测验,但我 运行 遇到了问题。
在 displayQuestion
方法中调用 guess
方法。在 guess
方法中增加 questionsIndex
值,然后再次调用 displayQuestion
方法。
这个过程不断重复,直到 questionIndex
变为 out of range
。看来您需要从 guess
方法中删除对 displayQuestion
方法的调用。
我是运行下面写的代码块:
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()
我面临以下问题:
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
你能告诉我这是什么原因吗?为什么列表有问题?我添加这个是因为 Whosebug 希望我添加更多细节:我试图在这个软件中使用基本 class 方法构建一个测验,但我 运行 遇到了问题。
在 displayQuestion
方法中调用 guess
方法。在 guess
方法中增加 questionsIndex
值,然后再次调用 displayQuestion
方法。
这个过程不断重复,直到 questionIndex
变为 out of range
。看来您需要从 guess
方法中删除对 displayQuestion
方法的调用。