At My Fingertips
Rapid Playground
Have a look at these graphics:
A | B | C | D | E |
---|---|---|---|---|
We could call them battery indicators. Here are some similarities:
Now let's focus on their differences,
so we can figure out what parameters a battery_indicator
function should have.
def battery_indicator(...) -> Graphic:
We can see that the battery levels differ,
so we want to have a parameter to specify the level
(say, with values between 0.0 for an empty battery
and 1.0 for a full battery).
Besides the level, there's also the width and height that can be changed.
def battery_indicator(width: float, height: float, level: float) -> Graphic:
The remaining measures (such as the width of the positive terminal button sitting on the top) can be derived from the parameters.
Assume that width
and height
are the width and height of the body
(the black rounded rectangle) of the battery,
and that the positive terminal button sits above
(so the height of the overall graphic is height + width * 0.1
).
Import the rounded_rectangle
function from your toolbox
(if you don't have that function yet, build it in the Rounded Rectangle activity).
Besides rounded_rectangle
, use rectangle, overlay, and above.
Play with your battery_indicator
function,
creating indicators with different sizes and levels.
You just played with some rather extreme argument values. What happens if you go beyond those?
The code produced an error.
Unfortunately, when someone who calls your function sees that error,
it is not clear who to blame.
They might blame you!
They might say that your battery_indicator
function is buggy!
You can shift the blame to them! You can show them that it was their mistake! They called your function with the wrong arguments!
To do that, add assertions at the start of your function. Those assertions ensure that the argument values are acceptable. If the assertions fail, you can show the error message and tell the person who called your function that they violated your preconditions.
In our case, the battery_indicator
function
does not accept a level
that is greater than 1.0.
Also, you may want to assert that the width and height have acceptable values (e.g., that they are not negative).
def battery_indicator(width: float, height: float, level: float) -> Graphic:
assert level >= ...
assert level >= ...
...
Note: You really want to fix that mistake now, because if you have any code cell in this activity that produces an error, you will not be able to move on.
Let's look at another possible problem.
The code should have produced an error (if your implementation set the wall thickness to 10% of the width). Can you figure out why?
Now save your battery_indicator
function in your toolbox.
It may come in handy as a component for more complex graphics in the future.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Battery Indicator
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)