Returns a string that represents a progress bar that has barWidth bars and has progressed progress amount out of a total amount.
(progress, total, barWidth=40)
| 30 | |
| 31 | |
| 32 | def getProgressBar(progress, total, barWidth=40): |
| 33 | """Returns a string that represents a progress bar that has barWidth |
| 34 | bars and has progressed progress amount out of a total amount.""" |
| 35 | |
| 36 | progressBar = '' # The progress bar will be a string value. |
| 37 | progressBar += '[' # Create the left end of the progress bar. |
| 38 | |
| 39 | # Make sure that the amount of progress is between 0 and total: |
| 40 | if progress > total: |
| 41 | progress = total |
| 42 | if progress < 0: |
| 43 | progress = 0 |
| 44 | |
| 45 | # Calculate the number of "bars" to display: |
| 46 | numberOfBars = int((progress / total) * barWidth) |
| 47 | |
| 48 | progressBar += BAR * numberOfBars # Add the progress bar. |
| 49 | progressBar += ' ' * (barWidth - numberOfBars) # Add empty space. |
| 50 | progressBar += ']' # Add the right end of the progress bar. |
| 51 | |
| 52 | # Calculate the percentage complete: |
| 53 | percentComplete = round(progress / total * 100, 1) |
| 54 | progressBar += ' ' + str(percentComplete) + '%' # Add percentage. |
| 55 | |
| 56 | # Add the numbers: |
| 57 | progressBar += ' ' + str(progress) + '/' + str(total) |
| 58 | |
| 59 | return progressBar # Return the progress bar string. |
| 60 | |
| 61 | |
| 62 | # If the program is run (instead of imported), run the game: |