Calculator
Step 1
Write a command line program, that accepts dual-operand infix-only mathematical equations and calculates the results, in an endless loop.1
You may assume the user input has the structure: operand 1, space, operator, space, operand 2 like so 1 + 5
or 6 - 8
.2
It should at least accept +, -, *, / as operators.3 4 5
Example:
Step 2
Log6 your calculations and their results in a textfile.
Use the following format: 1 + 5 = 6
7
-
A
while
loop would be appropriate here. ↩ -
Since we assume operands and operator are separated by spaces, we can split the string at the spaces.
input.split(' ')
↩ -
Remember to cast the operands to an int or even better float, once you have split it. ↩
-
Also remember, that you can pack multiple if/else commands:
if condition1: # commands elif condition2: # commands #there is no limit to the number of elifs else: # normal case
-
Do not forget to catch the
ZeroDivisionError
Exception! ↩ -
You can log it using append mode:
open(filename, 'a')
↩ -
Do not forget to add a newline at the end of every line, ↩