Simple Calculator
Python
30 pts
Simple Calculator
Each task is a tuple (a, operator, b). Loop through tasks and use match on the operator:
"+","-","*": calculate the result."/"with a guardif b != 0: calculatea / b."/"(whenbis 0): the result is the text"Cannot divide by zero".- Anything else: the result is
"Unknown operator".
Append every result to a list called results and print each line as shown.
Expected output:
12 + 8 = 20 20 - 5 = 15 6 * 7 = 42 9 / 0 = Cannot divide by zero 15 / 3 = 5.0 2 ^ 3 = Unknown operator
Checklist
- Add one result to
resultsfor every task. - Handle addition, subtraction and multiplication.
- Handle division, including division by zero, with a guard.
- Handle any other operator.
- Print each calculation as shown.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
