Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write a function calc(a, op, b) for +, -, * and /. For division by zero it should give a message, not an error. What are calc(6, "+", 3), calc(6, "/", 4) and calc(6, "/", 0)?
Test for the dangerous case before you divide.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. An if, elif ladder on the sign. In the division branch, test b == 0 first.
calc(6, "+", 3) is .calc(6, "/", 4) is .calc(6, "/", 0) gives the message cannot divide by zero.Any other sign gives unknown operation.
Log in to join the discussion.
Loading…