我正在制作一个游戏,并且在代码中有两个类。一个定义问题,另一个定义 4 个多项选择答案。这就是我所拥有的:
class new_question(type):
""" The Question that displays on screen """
def __init__(self, question):
super(new_question, self).__init__(question = question)
def ask_quest(self):
global QUESTION
QUESTION = ask_question
QUESTION.value = question
那是我的第一堂课,我的第二堂课是:
class answer(type):
""" Four answers that display in their own boxes """
def __init__(self, answers):
super(answer, self).__init__(answers = answers)
def all_answers(self):
global ANS1
global ANS2
global ANS3
global ANS4
ANS1 = poss_ans_1
ANS1.value = answers[0]
ANS2 = poss_ans_2
ANS2.value = answers[1]
ANS3 = poss_ans_3
ANS3.value = answers[2]
ANS4 = poss_ans_4
ANS4.value = answers[3]
所有变量都在此文件的其他地方和其他地方定义,但这不是我遇到的问题。当我去调用这些类时,我认为最好的办法是在我的主循环中从类中调用单个函数:
def main():
load_image()
ans = answer(type)
ans.all_answers()
main()
但是,当我运行程序时,我收到此错误:
Traceback (most recent call last):
File "C:\Users\Roger\Documents\Trivia New\main.py", line 83, in <module>
main()
File "C:\Users\Roger\Documents\Trivia New\main.py", line 82, in main
ans.all_answers()
AttributeError: type object 'type' has no attribute 'all_answers'
我不确定发生了什么,但我已经在这个问题上遇到了 3 个小时,但仍然无法弄清楚。如果有人可以帮助我,我将不胜感激。