![](/img/trans.png)
[英]Patch a function call within function from different file using Python
[英]Error using end=" " within a function call
以下代碼用於向用戶發送消息:
mud.send_message(id, rooms[self.current_room]["desc"])
在游戲代碼的一部分中,我不想從新行開始,所以我嘗試:
mud.send_message(id, rooms[self.current_room]["desc"], end=" ")
mud.send_message(id, "This starts on the same line as the code above.")
這當然會引發一個錯誤,即這里不歡迎第三個變量 (end="")。 如何在同一行開始第二條消息?
如果需要,額外的信息:
def send_message(self, to, message):
self._attempt_send(to, message+"\n\r")
您想到的end
參數特定於內置print
功能; 其他的事情,輸出文本不一定會支持它。
如果send_message
是您自己的代碼,那么您可以修改它以不自動添加換行符 - 甚至實現end
參數(如果需要,我可以添加詳細信息)。
如果send_message
在其他人的庫中,那么通常您應該先檢查該庫的文檔並查看推薦的內容。
但是,對於像這個這樣簡單的情況,顯而易見的事情就是准備一行文本用於輸出,因此只進行一次send_message
調用。
例如,您可以使用字符串格式執行此操作:
# Python 3.6 and later
mud.send_message(id, f'{rooms[self.current_room]["desc"]} This is on the same line.')
# Earlier 3.x, before the introduction of f-strings
mud.send_message(id, '{} This is on the same line.'.format(rooms[self.current_room]["desc"]))
由於send_message
總是將'\\n\\r'
到您傳遞給它的任何message
,您可以調用_attempt_send
代替:
mud._attempt_send(id, rooms[self.current_room]["desc"] + " ")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.