1

The following code works to send a message to a user:

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

In one part of the game code, I don't want to start on a new line so I try:

mud.send_message(id, rooms[self.current_room]["desc"], end=" ")
mud.send_message(id, "This starts on the same line as the code above.")

This of course raises an error that the third variable (end=" ") is not welcome here. How can I start the second message on the same line?

Extra info if needed:

def send_message(self, to, message):
    self._attempt_send(to, message+"\n\r")
Dr J
  • 135
  • 11
  • Well, if you want to have an `end` parameter, why don't you add it to the `send_message` function, and use it to control whether `\n\r` is added or not? – mkrieger1 Sep 22 '18 at 16:05
  • Is the `send_message` function part of the code that you wrote yourself? If not, where did you get the code, and *what does the documentation say*? – Karl Knechtel Sep 22 '18 at 16:22
  • @mkrieger1 Because I want this to happen at specific places, not on every message. – Dr J Sep 22 '18 at 17:08
  • @KarlKnechtel The documentation simply states: "we make sure to put a newline on the end so the client receives the message on its own line", which is fine in most cases. – Dr J Sep 22 '18 at 17:08
  • @mkrieger1 What I meant was that 99% of the time, I want the new line. There are few instances that I don't want the new line so manually adding the \n in the 99% of those cases would be inefficient for my needs. – Dr J Sep 22 '18 at 17:16

2 Answers2

2

The end parameter that you have in mind is specific to the built-in print function; other things that output text will not necessarily support it.

If send_message is your own code, then you could modify it not to add the newline automatically - or even to implement the end parameter (I can add details if desired).

If send_message is in someone else's library, then in general you should check the documentation for that library first and see what is recommended.

However, for a case as simple as this one, the obvious thing to do is just prepare a single line of text for output, so that only one send_message call is made.

You can do this for example with string formatting:

# 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"]))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Beautiful, this was the syntax I couldn't figure out. See above as to why I don't want to add it to every message, i.e. the main send_message code. – Dr J Sep 22 '18 at 17:14
-1

Since send_message always concatenates '\n\r' to any message you pass to it, you can call _attempt_send instead:

mud._attempt_send(id, rooms[self.current_room]["desc"] + " ")
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • -1: While this would work, the leading `_` on this method name strongly indicates it is not intended to be called directly. (It seems likely that OP has gone to examine the code of a third-party library.) – Karl Knechtel Sep 22 '18 at 16:28