1
我正在做一個遊戲,並在代碼兩個類。一個定義了問題,另一個定義了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小時這個同樣的問題,現在,仍然無法搞清楚。如果有人能幫助我,我將不勝感激。
我想你應該調用ans = answer(type,** answers **)答案是你的答案列表,因爲答案沒有帶單個參數的構造函數。 – njzk2
在類中使用'global'方法打破了封裝和編碼可重用性的所有想法。嘗試編寫沒有'global'的代碼。有用。真。 – Matthias