How to make a still progress in python? -
i need make progress bar in python prints out @ x%
.
so, example, if value 62%, there way print out this?
▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯ 62%
or, example, 23%
▮▮▮▮▮▮▮▮▮▮▮▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯ 23%
a pure solution using end
parameter of print()
:
the print()
statement allows specify last chars
printed @ end of call. default, end
set '\r\n'
carriage return , newline. moves 'cursor' start of next line want in cases.
however progress bar, want able print on bar can change current position , percentage without end result looking bad on multiple lines.
to this, need set end
parameter carriage return: '\r'
. bring our current position start of line can re-print
on progress bar.
as demonstration of how work, made simple little code increase progress bar 1%
every 0.5
seconds. key part of code print
statement part take away , put main script.
import time, math bl = 50 #the length of bar p in range(101): chars = math.ceil(p * (bl/100)) print('▮' * chars + '▯' * (bl-chars), str(p) + '%', end='\r') time.sleep(0.5)
(n.b. chars used here copied question ('▮, ▯') , me did not print console in case need replace them other ones example: '█' , '.')
this code want , steadily increases percentage, here example of how when p = 42
:
▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯▯ 42%
Comments
Post a Comment