Code Understanding:
The function View_bar (num,total) num is a random number, Total is the number (num/total) * -the int type can calculate the percentage'\r%d%%%s'% (Rate_num,'='*num) \ r represents always output at the beginning of a line%D represents the percentage received as a number in the form of a rate_num in parentheses.Percent%, to output special characters%need to be added one for escaping. %s with the following'='The *num is matched to output a certain amount of =Sys.stdout.write (R) indicates that no line break is required after the output line, the direct output can be. Sys.stdout.flush () represents emptying the console content, which is used in conjunction with Sys.stdout.write (R) to achieve the effect of always outputting a percentage number at the beginning of one line. Time.sleep in the For loop (0.3represents no 0.3 seconds to give a number to calculate the percentage
Code section:
Import sysimport timedef View_bar (num,total): rate= num/Total rate_num=int(Rate * -) R='\r%d%%%s>'% (Rate_num,'='*num) sys.stdout.write (R) Sys.stdout.flush ()if__name__ = ='__main__': forIinchRange1,101): Time.sleep (0.3) View_bar (i, -)
Execution Result:
%====================>wuyi%===================================================>100 %====================================================================================================>
Python Progress bar Small instance