0% found this document useful (0 votes)
13 views2 pages

Calculator Aym

The document provides a simple calculator program in Python that can perform addition, subtraction, multiplication, and division. It includes functions for each operation and a main loop that allows users to select an operation and input numbers. The program also handles invalid inputs and offers the option to perform additional calculations.

Uploaded by

ayanislam0973
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Calculator Aym

The document provides a simple calculator program in Python that can perform addition, subtraction, multiplication, and division. It includes functions for each operation and a main loop that allows users to select an operation and input numbers. The program also handles invalid inputs and offers the option to perform additional calculations.

Uploaded by

ayanislam0973
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Here you can make a calculator easily !

# Function to add two numbers


def add(x, y):
return x + y

# Function to subtract two numbers


def subtract(x, y):
return x - y

# Function to multiply two numbers


def multiply(x, y):
return x * y

# Function to divide two numbers


def divide(x, y):
if y == 0:
return "Error! Division by zero."
return x / y

# Main function for the calculator


def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

while True:
# Taking input from the user
choice = input("Enter choice (1/2/3/4): ")

# Check if the choice is valid


if choice in ('1', '2', '3', '4'):
try:
# Get user input for numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input! Please enter numeric values.")
continue

# Perform the chosen operation


if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")

elif choice == '2':


print(f"{num1} - {num2} = {subtract(num1, num2)}")

elif choice == '3':


print(f"{num1} * {num2} = {multiply(num1, num2)}")

elif choice == '4':


print(f"{num1} / {num2} = {divide(num1, num2)}")

else:
print("Invalid Input! Please select a valid operation.")

# Ask the user if they want to perform another calculation


next_calculation = input("Do you want to perform another calculation?
(yes/no): ").lower()

if next_calculation != 'yes':
print("Goodbye!")
break

# Run the calculator


calculator()

You might also like