playerList
包含兩個Player
對象(具有str
屬性稱爲分別"a"
和"b"
),和Player
器具__str__
和__repr__
。當我將str(playerList)
連接到另一個字符串時,我期望字符串被"[a, b]"
的形式附加。相反,結果字符串附加"[ , ]"
。我做了什麼錯誤,得出了這個結果?爲什麼對`str(someList)`的調用有時會創建一個空格「數組」?
這是我寫的
prompt = "And then choose the opponent you would like to attack from " + str(playerList)
def __str__ (self):
return self.name
def __repr__ (self):
return str()
我得到在標準輸出上:
"And then choose the opponent you would like to attack from [, ]"
我想要什麼:
"And then choose the opponent you would like to attack from [a,b]"
return str()?不應該是自我.__ str__? –
尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證的示例。](http://stackoverflow.com/help/mcve) –
希望更新是好的嗎? – Muno