Certainly! Below is an example of a simple calculator program written in Python. This calculator can perform basic operations such as addition, subtraction, multiplication, and division. To keep things straightforward, I'll structure it to accept input from the user, perform the requested operation, and then display the result.
# Simple Calculator Program
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error! Division by zero."
else:
return x / y
# Function to manage the calculator's operation
def calculator():
print("Welcome to the simple calculator!")
while True:
print("Operations:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
choice = input("Select operation (1/2/3/4/5): ")
if choice == '5':
print("Thank you for using the calculator. Goodbye!")
break
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result: ", add(num1, num2))
elif choice == '2':
print("Result: ", subtract(num1, num2))
elif choice == '3':
print("Result: ", multiply(num1, num2))
elif choice == '4':
print("Result: ", divide(num1, num2))
else:
print("Invalid Input")
# Ask the user if they want to perform another calculation
another_calculation = input("Would you like to do another calculation? (yes/no): ")
if another_calculation.lower() != 'yes':
print("Thank you for using the calculator. Goodbye!")
break
if __name__ == "__main__":
calculator()
To run this program, you'll need Python installed on your computer. You can then save the code to a file with a .py extension, for example, simple_calculator.py, and run it from your command line or terminal by navigating to the directory containing the file and typing python simple_calculator.py.