Reference link: Python | end parameter in print()
Let's take a closer look at the parameters of print: print(value,sep='',end='\n',file=sys.stdout,flush=False)
The value is the string we want to print, and sep is the interval between values (we can print ("Hello", "Python") to see that there is indeed a space in between)
end is the thing to be printed after printing is completed, print defaults to print a \n at the end, that is, a newline (we want print not to wrap, just change the end parameter to'').
Where is file=sys.stdout printed? sys.stdout is the console of the system, which is the standard output device
Flush=False means that print does not open the buffer. We only need to set flush to True to open the buffer.
Countdown program, here 5 seconds countdown
import time
print("Countdown program")
for x in range(5,-1,-1):
mystr = "countdown" + str(x) + "seconds"
print(mystr,end = "")
print("\b" * (len(mystr)*2),end = "",flush=True)
time.sleep(1)
To explain:
Range(5,-1,-1) means: use range to generate a list, starting from 5 and ending before -1. The usage of range is range (start, end, step size), because we want to go from big to small, so we use -1 to indicate that the step is -1, which means -1 every time, if it is -2, then every Times-2
str(x) converts the x variable into a string
print(mystr,end = "") After we print the string, we don’t wrap, that is, end=""
The most important statement: print("\b" * (len(mystr)*2),end = "",flush=True)
"\B" * (len(mystr)2) means to print the escape character'\b', and then print len(mystr) 2 times. len gets the length of the string, why 2? You know, the string we are using is Chinese, and 1 Chinese character = 2 English characters (placeholder), so if the string is English, we can completely not 2, but the Chinese characters are different. Specific friends can try to remove *2
flush = True is to open the buffer
\ The b escape character has a backspace function, which is equivalent to pressing the BackSpace key when editing a file to delete a character from the cursor position forward
time.sleep(1) is to pause the program for 1 second
In this way, after each print, \b helps us to clear all the characters in a line. This is why we need to obtain the string length. Also, Python's IDLE cannot recognize the \b character, so we The correct result can only be seen in the console.
import time
print("Display percentage")
for x in range(101):
mystr = "percent" + str(x) + "%"
print(mystr,end = "")
print("\b" * (len(mystr)*2),end = "",flush=True)
time.sleep(0.5)
import time
input("Welcome to "Time Manager"! Please press Enter to continue.")
while True:
task_name = input('Please enter the task name:')
task_time = int(input('How many minutes do you think you can focus on this task at least? Enter N minutes'))
input('This task information:\nThe task I want to complete: %s\nI must at least concentrate: %d minutes\nPress Enter to start timing:'%(task_name,task_time))
start = time.time() # start timing
start_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # format date
# for t in range(task_time*60,0,-1):
for t in range(task_time,0,-1):
info ='Please focus on the task, and stay focused' + str(t) + 'seconds! '
print(info,end="")
print("\b"*(len(info)*2),end="",flush=True)
time.sleep(1)
print('You have focused on %d minutes, great~ Work harder and complete the task!'%task_time) # After the countdown, continue to run the subsequent code.
Recommended Posts