Reputation: 3
I wanted to do like a . .. ... thing to show the Programm is "thinking".
So i came up with an idea:
import time
print(".", time.sleep(x), ".", time.sleep(x), ".")
But of course it didn't work.
Doing:
import time
time.sleep(0.8)
print(".")
time.sleep(0.8)
print("..")
time.sleep(0.8)
print("...")
time.sleep(0.8)
Puts all the dots under each other.
Does anyone have an idea how it could work?
Thank you in advance.
Upvotes: 0
Views: 856
Reputation: 2498
Unfortunately your approach won't work. Comma separated values in the print statement don't allow for new pieces of logic; they just tell python to print space separated strings. For this you need to play around with the print statement parameters a little bit.
The code is:
import time
i = 1
while i < 4:
dots = '.'*i
print("\r{}".format(dots), flush=True, end='')
time.sleep(1)
i += 1
You don't necessarily need the while loop, but to understand what's happening in the print statement. You must include the "\r", which is carriage return, at the start of the string. This tells Python after printing to return the cursor to the start of the line, for the next time it needs to print.
In the parameters for the print function, use "end=''", which tells python to end with empty string. By default the print statement ends with a newline, which is unwanted behaviour.
Finally, "flush=True" tells Python to delete anything in front of what needs to be printed. This is necessary because you will have used carriage return to go to the start.
Upvotes: 1