write a program to make a mini calculator in python.
# Mini Calculator
# Function to add two numbers
def add(num1, num2):
return num1 + num2
# Function to subtract two numbers
def subtract(num1, num2):
return num1 - num2
# Function to multiply two numbers
def multiply(num1, num2):
return num1 * num2
# Function to divide two numbers
def divide(num1, num2):
return num1 / num2
# Main program
print("Welcome to the Mini Calculator!")
print("Select an operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
# Get user input for operation
operation = int(input("Enter your choice (1-4): "))
# Get user input for numbers
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
# Perform the selected operation
if operation == 1:
result = add(number1, number2)
operator = "+"
elif operation == 2:
result = subtract(number1, number2)
operator = "-"
elif operation == 3:
result = multiply(number1, number2)
operator = "*"
elif operation == 4:
result = divide(number1, number2)
operator = "/"
else:
print("Invalid operation!")
exit()
# Print the result
print("Result: {} {} {} = {}".format(number1, operator, number2, result))
Comments
Post a Comment