Python: Syntactic Sugar
Constructions like index = index + 1 are often found in Python, so the creators of the language added an abbreviated version: index += 1.
The only difference is how they're written. The interpreter will turn an abbreviated construction into its expanded form.
These abbreviations are called syntactic sugar because they make the process of writing code a little easier and more pleasant.
This form is especially common in loops, where we often change a counter and accumulate a result:
sum = 0
index = 1
while index <= 5:
sum += index # Same as sum = sum + index
index += 1 # Same as index = index + 1
print(sum) # => 15Without abbreviated assignment, the loop body would be longer:
while index <= 5:
sum = sum + index
index = index + 1There are abbreviated forms for all arithmetic operations and for string concatenation:
a = a + 1→a += 1a = a - 1→a -= 1a = a * 2→a *= 2a = a / 1→a /= 1
Instructions
Implement the function build_progress_bar(), which takes the number of completed steps and the total number of steps, then returns a progress indicator string.
Completed steps are shown with #, and remaining steps are shown with -. Try not to use built-in string methods in your solution.
build_progress_bar(0, 5) # '-----'
build_progress_bar(3, 5) # '###--'
build_progress_bar(5, 5) # '#####'If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Python: Syntactic Sugar
Constructions like index = index + 1 are often found in Python, so the creators of the language added an abbreviated version: index += 1.
The only difference is how they're written. The interpreter will turn an abbreviated construction into its expanded form.
These abbreviations are called syntactic sugar because they make the process of writing code a little easier and more pleasant.
This form is especially common in loops, where we often change a counter and accumulate a result:
sum = 0
index = 1
while index <= 5:
sum += index # Same as sum = sum + index
index += 1 # Same as index = index + 1
print(sum) # => 15Without abbreviated assignment, the loop body would be longer:
while index <= 5:
sum = sum + index
index = index + 1There are abbreviated forms for all arithmetic operations and for string concatenation:
a = a + 1→a += 1a = a - 1→a -= 1a = a * 2→a *= 2a = a / 1→a /= 1
Instructions
Implement the function build_progress_bar(), which takes the number of completed steps and the total number of steps, then returns a progress indicator string.
Completed steps are shown with #, and remaining steps are shown with -. Try not to use built-in string methods in your solution.
build_progress_bar(0, 5) # '-----'
build_progress_bar(3, 5) # '###--'
build_progress_bar(5, 5) # '#####'If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import solution
def test1():
assert solution.build_progress_bar(0, 5) == "-----"
assert solution.build_progress_bar(3, 5) == "###--"
assert solution.build_progress_bar(5, 5) == "#####"Teacher's solution will be available in:
20:00
