MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / getProgressBar

Function getProgressBar

src/gamesbyexample/progressbar.py:32–59  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

30
31
32def 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:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected