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")
5
  • 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? Commented Sep 22, 2018 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? Commented Sep 22, 2018 at 16:22
  • @mkrieger1 Because I want this to happen at specific places, not on every message. Commented Sep 22, 2018 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. Commented Sep 22, 2018 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. Commented Sep 22, 2018 at 17:16

2 Answers 2

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"]))
Sign up to request clarification or add additional context in comments.

1 Comment

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.
-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"] + " ")

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.