Search code examples
pythoncommand-linegame-engine

Is there a way to prevent automatic newline on python command line?


I am making a command line game engine in python. However, when I attempt to print a command, it newlines and creates a jittery frame.

Adding the end attribute bogs down my computer and nearly crashes the shell. The dupe uses sys.stdout.write('') newline sys.stdout.flush or print'', or print('',end=''). all bog down shell and crash it. print('') doesn't bog down though which is weird.

#this is enough code to demonstrate the problem
while true:
    print('                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         =                                                                              ===                                                                            =====                                                                          =======                                                                        =========                                                                      ===========                                                                    =============    YYYYYYYYYYY                                                 ================================================================================')
#crash issue
import sys
while True:
    sys.stdout.write('mooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo')
    sys.stdout.flush()

I expect the screen to fill, instead it wobbles up and down.


Solution

  • I am not sure if I understood your question correctly, but I’m thinking you do not want to print a new line with each call to the print() function.

    If so, the print function has an optional argument end, that is set by default as \n (this, creating a new line if not specified otherwise). If you don’t want this to happen, you can simply use the print function as:

    print(your_argument, end=“”)      
    

    Replacing your_argument with whatever you want to print out.