Python Progress bar Small instance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.