我正在運行下面撰寫的代碼塊:
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
你能告訴我這是什么原因嗎?為什么串列有問題?我添加這個是因為 stackoverflow 希望我添加更多詳細資訊:我嘗試使用該軟體中的基本類方法構建測驗,但遇到了問題。
uj5u.com熱心網友回復:
在displayQuestion方法中你呼叫guess方法。在guess方法中增加questionsIndex值,然后再次呼叫displayQuestion方法。
這個程序不斷重復,無限重復,直到questionIndex完成out of range。看來您需要displayQuestion從方法中洗掉呼叫該guess方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/441392.html
標籤:Python python-3.x 班级
