Projects‎ > ‎

Project 5

You are creating a simple interactive calculator. Once your script runs, the user gets a simple prompt (something like ">>"), and can enter a calculation. Your script then prints the answer and repeats the prompt. You need to implement the following operations:
  • Addition; user enters: 3 + 4
  • Subtraction; user enters: 3 - 4
  • Multiply; user enters: 3 * 4
  • Divide; user enters: 3 / 4
  • 3 different logarithms:
    • Natural Log (base e); user enters: ln(3)
    • Log base 10; user enters: log(3)
    • Log base 2; user enters: log2(3)
Notes: 
  • You need to implement compound expressions to one level: 3+log(2) is required; 3+log(1+2) is not - neither is log(log(5))
  • The logarithms are implemented in the "math" module
  • You must handle floating point numbers correctly
Two key requirements:
  1. To keep the script "clean" and readable create (at least) two functions: one that handles the basic arithmetic and one that handles the logarithms.
  2. Nothing the user enters should cause your script to crash; if there is an error you need to print a message and return to the prompt.  Your script exits when the user type one of "x", "q", "exit", or "quit".
Build the basic calculator first, and test it with correct input. Them, focus on making your script crash with incorrect and unexpected input, and guard against these crashes.  Try to do this as efficiently as possible, a lot of "if" statements are not the best way to go. The "Try" construct is recommended.