I'm a beginner and working on a very basic skill set.
I'm making a simple text game in command line for windows, and have a function that lets users read the most recent statement, and skip it by causing a KeyboardInterrupt like with Ctrl-C.
from time import sleep
def wait(seconds):
try:
sleep(seconds)
except KeyboardInterrupt:
pass
return
the issue arises when I want to print something and not have a newline afterwards. In that case, the wait() function will execute before the print() function
# functions properly, but has unwanted newline
print("test", end='test\n')
wait(3)
# in windows CMD, wait() executes before print()
print("test", end='test')
wait(3)
I know there are ways around this like using TKinter, but I want to know why this happens, not how to avoid it entirely.
EDIT: I kept searching and found the issue wasn't the try except block, but sleep(): Error with Print and Sleep in Python copy of the answer:
You should use:
print (a, end="", flush=True)
Because console output is line-buffered.