[英]Python error of class regarding attributes and objects
我写了以下代码:
class QuizBrain:
def __init__(self,q_list):
self.question_number = 0
self.question_list = q_list
def next_question(self):
current_question = self.question_list[self.question_number]
self.question_number += 1
user_input = input(f"Q.{self.question_number} : {self.current_question} (True/False) : ")
但是,当我创建此 class 的 object 并访问object.next_question()
时,它会显示此错误:
Traceback (most recent call last):
File "main.py", line 14, in <module>
quiz.next_question()
File "/home/runner/quiz-game-start/quiz_brain.py", line 10, in next_question
user_input = input(f"Q.{self.question_number} : {self.current_question} (True/False) : ")
AttributeError: 'QuizBrain' object has no attribute 'current_question'
在编写代码时, current_question
只是一个局部变量,而不是 class 属性。 将其更改为self.current_question
以转换为属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.