Improving the calculator
Goal
Previous Python Code w/ Commentary
# Basic Calculator
# Input Handling
number1 = int(input("Enter number 1: "))
number2 = int(input("Enter number 2: "))
# Since we convert the inputs to integers, we cannot handle inputs with decimals.
# There is no feature built-in to our code to prevent non-numeric inputs at the moment.
# Operations
result1 = number1 + number2
result2 = number1 - number2
result3 = number1 * number2
result4 = number1 / number2
# Instead of asking the user which operation to perform, we execute all the operations.
# Output
print(f"{number1} + {number2} = {result1}")
print(f"{number1} - {number2} = {result2}")
print(f"{number1} * {number2} = {result3}")
print(f"{number1} / {number2} = {result4}")Extended Requirements
User Chooses Desired Operation
Code 1 Explanation:
Numeric Inputs Only + Supporting Decimals
Code 2 Explanation:
Restrict Division by Zero
The Full Final Code
Code Explanation:
Key Points:
Last updated