Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用end=";时出错&引用;在函数调用中_Python_Python 3.x - Fatal编程技术网

Python 使用end=";时出错&引用;在函数调用中

Python 使用end=";时出错&引用;在函数调用中,python,python-3.x,Python,Python 3.x,以下代码用于向用户发送消息: 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.") 这当然会引发一个错误,

以下代码用于向用户发送消息:

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")

由于
send\u message
始终将
'\n\r'
连接到传递给它的任何
消息
,因此您可以调用
\u trunt\u send

mud._attempt_send(id, rooms[self.current_room]["desc"] + " ")

您想到的
end
参数特定于内置的
print
功能;其他输出文本的东西不一定支持它

如果
send\u message
是您自己的代码,那么您可以修改它以不自动添加换行符,甚至实现
end
参数(如果需要,我可以添加详细信息)

如果
send_message
在其他人的库中,则通常应首先检查该库的文档,并查看推荐内容

然而,对于这样一个简单的例子,显然要做的就是准备一行文本用于输出,这样就只进行一次
send\u 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"]))

好的,如果您想有一个
end
参数,为什么不将它添加到
send\u message
函数中,并使用它来控制是否添加
\n\r
呢?
send\u message
函数是您自己编写的代码的一部分吗?如果没有,代码是从哪里来的?文档上说了什么?@mkrieger1因为我希望这种情况发生在特定的地方,而不是每封邮件上。@KarlKnechtel文档只是说:“我们确保在末尾放一个换行符,以便客户机在自己的行上接收邮件”,在大多数情况下这很好。@mkrieger1我的意思是,99%的时候,我想要新的线路。很少有实例我不想要新行,因此在99%的情况下手动添加\n将无法满足我的需要。-1:虽然这会起作用,但此方法名称上的前导
\uuu
强烈表示不打算直接调用它。(似乎OP已经去检查第三方库的代码了。)很好,这是我无法理解的语法。请参见上文,了解我不想将其添加到每条消息(即主发送消息代码)中的原因。