Basic_Calculator
Code:
def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
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.