Flow controls: Conditions · 22 / 22 Exercise Hard

Menu with match

Objective

The match statement (Python 3.10 or later) compares a value against several patterns, even structures such as lists. It is ideal for interpreting commands such as "add 2 3".

Instructions

  • Read a line and split it with split() into a list.
  • Use match with the patterns ["add", a, b], ["sub", a, b] and ["mul", a, b] to add, subtract and multiply integers and show Result: x.
  • With ["div", a, b] divide showing the result with two decimals, but if the divisor is "0" show Division by zero (put that case first).
  • Any other command (case _:) shows Unknown operation.
Input
add 2 3
Output
Result: 5

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise