Write down whatever you mess up on in a notebook and write down the solution that worked. If you weren't able to finish, write down a guess.
Goal: Ask for two numbers and an operator (+, -, *, /).
- Use
input()three times to collect:- First number
- Operator
- Second number
- Convert the numbers from strings to a number.
- Print all three inputs (e.g.,
5 + 3.0). Don't solve yet, just print it.
Extension:
What if you want to write 0.5 + 0.5? Which type is right to cast to?
Goal: Perform calculations based on the operator.
- Check the operator and do the operation. Hint: use multiple ifs.
- You can't divide by 0, ask google why if you don't understand it. Handle division by zero (print "Error: Can't divide by 0!").
Hint:
- Add
**(exponent) and%(modulo) as new operators. - Don't understand what they mean? Before Googling, just add the if statements for these operations and try plugging in random numbers.
Goal: Ensure inputs are valid.
- Check if the numbers are actually numbers (Hint: Google how)
- Print an error if the operator isn't valid.
- If invalid, print "Invalid input!" and stop.
- Test
operator=4and other weird code. What does it do?
Goal: Let the user keep calculating until they say "quit".
- Wrap the calculator in a
while True:loop. Find out what this does. - Ask after each calculation: "Continue? (yes/no)".
- Exit the loop if they type "no".
Hint:
- Google
breakstatement.
Goal: Save the last 5 calculations using variables.
This problem is challenging! Highly suggest you draw the flow of the code on paper first.
- Create 5 variables:
hist1 = "" hist2 = "" hist3 = "" hist4 = "" hist5 = ""
- After each calculation, update the history:
- Shift old entries (e.g.,
hist5 = hist4,hist4 = hist3, ...) - Save the newest to
hist1:"5.0 + 3.0 = 8.0"
- Shift old entries (e.g.,
- Print all 5 entries after each calculation.
Debug Challenge:
- What happens if you do
hist5 = hist4after updatinghist1?