Reputation: 33
I am working on a text based game but am very new to programming, I want to do something like
from time import sleep
print('first part' sleep(1)'second part')
but that obviously does not work so I was wondering if I could print one part of the line, wait, and then print the second part to the same line but from different lines so that it would work
Upvotes: 1
Views: 2090
Reputation: 82
Quick Function to do this. You just have to put your strings into a list.
import time
def PrintWithPauses(lists):
for x in range(len(lists)):
if x == len(lists):
print(lists[x])
else:
print(lists[x], end='', flush=True)
time.sleep(1)
So if you had a list like this:
myList = ['hello world! ', 'StackOverflow ', 'bob ']
and ran:
PrintWithPauses(myList)
Your output would be:
hello world!
Pause 1 second
hello world! StackOverflow
Pause 1 second
hello world! StackOverflow bob
Upvotes: 0
Reputation: 102842
Sure, this is possible, you'll just have to send some options to print()
:
from time import sleep
print("This is the first part. ", end='', flush=True)
sleep(1)
print("This is the second part.")
end=''
means that the print
function doesn't append a newline \n
onto the end of what it's printing. flush=True
means the buffer is flushed right away, so it's immediately printed. Experiment with removing that part and see what happens.
Upvotes: 4