Basic Calculator
Basic Calculator
Code:
def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
if choice == '1':
print(f"Result: {num1 + num2}")
elif choice == '2':
print(f"Result: {num1 - num2}")
elif choice == '3':
print(f"Result: {num1 * num2}")
elif choice == '4':
if num2 != 0:
print(f"Result: {num1 / num2}")
else:
print("Cannot divide by zero.")
else:
print("Invalid input")
calculator()
Explanation:
This script implements a basic calculator that allows the user to add, subtract, multiply, or
divide two numbers.
The program prompts the user to select an operation, enter two numbers, and then
performs the corresponding calculation.
It also handles division by zero errors gracefully.