Python Print print timer function

Reference link: Python | end parameter in print()

  1. Countdown

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.

  1. Program that shows percentage progress

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)

  1. Case

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

Actual code: Multiply 60 minutes into seconds, use -1 to count down.

# 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

Python Print print timer function
How Python implements the timer function
Python enumerate() function
Python function buffer
Python custom function basics
Join function in Python
print statement in python
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Python3 built-in function table.md
Python defines a function method
Python high-order function usage summary!
Python realizes online translation function
Python tornado upload file function
Python magic function eval () learning
How Python implements FTP function
Python implements image stitching function
Python high-order function usage summary!
Python telnet login function implementation code
How Python implements the mail function
Why doesn't Python support function overloading?
Is there function overloading in python
Learning Python third day one-line function
Python ATM function implementation code example
Python realizes image recognition car function
Python implements ftp file transfer function
Python realizes udp transmission picture function
How to run id function in python
What is the id function of python
What is an anonymous function in Python
python3 realizes the function of mask drawing
What is the function body of python
Is there a helper function in python