這裏是我的代碼。Python我的類拋出TypeError
import fileinput, random
from os import system as sys
from sys import exit
class crazy8(object):
question = raw_input("please enter a yes or no question \n")
def fortune(self, filex, current):
current = r"./"
fortunes = list(fileinput.input(filex))
sys("cd", current)
print random.choice(fortunes)
crazy8.fortune(r"./crazy8")
exit(0)
當我運行程序時,我輸入一個問題(我知道程序不在乎輸入什麼)。我想我在課上做錯了什麼。我知道當沒有class:語句時它工作正常,但我需要那裏的類(在完成之後,我將使用它作爲模塊)。
的問題後,我得到
TypeError: unbound method fortune() must be called with crazy8 instance as first argument (got str instance instead)
(我沒加任何錯誤又檢查。我將嘗試添加try和catch /籌集如果文件./crazy8不存在。此外,我後來要添加一個會自動運行sys(「touch./crazy8」)(在Mac/linux上)的文件,並且在我發現如何在Windows上創建文件後,我會添加該文件。