Session 1 of 6 - Basic Calculator
Overview
The calculator should take an input of 1, 2, 3, or 4, and if the user enters a number that is not 1, 2, 3, or 4, then the input line is reiterated until they enter a valid number. Depending on which input, the calculator is set to add, subtract, multiply, or divide. Then, the calculator accepts a list of numbers in CSV (comma-seperated values) and, after cleaning the input, executes the previously chosen operation on the numbers, and returns the final answer.
Example Output
[Terminal]: Do you want to add (1), subract (2), multiply (3), or divide (4)?:
[User]: 7
[Terminal]: Invalid input. Try again.
[User]: -200
[Terminal]: Invalid input. Try again.
[User]: 3
[Terminal]: Enter a list of numbers to multiply, seperated by commas:
[User]: 1,3, 4,t, 9
[Terminal]: 1 * 3 * 4 * 9 = 108
Improvements
- Make the calculator keep running when you finish a calculation so you can do multiple
calculations at once. Hint:
while continue == "Yes" - Add more operations, like powers (
a ** b) and remainder (a % b) - Make the final output look nicer. Hint:
\nadds a new line, fo example"New\nLine"=
"""
New
Line
"""
Instead of only using single quotes ("text") for normal strings, we can use triple quotes ("""text""") for multiline strings
Challenge
This calculator can only do one operation at a time, so if you wanted to do 3 + 2 + 6 * 4,
you'd have to run it 3 times:
- Once to do
3 + 2 - Then again to do
6 * 4 - And then finally to sum
5(3 + 2) and24(6 * 4) As a challenge, you can make it so that instead of taking an input for operation and numbers, you can just type an expression (a math sentence) like3 + 2 + 6 * 4, and it will detect where everything is, and what all the operations mean, and then output the answer, just like calculators in real life.
If the challenge seems too difficult for you, don't worry, it is made to extend your learning and make you critically think. Don't be afraid to ask for help, or use the internet, but instead of Google, use this JavaScript tutorial or this Python tutorial, depending on which we are using.