0% found this document useful (0 votes)
5 views1 page

# Simple Calculator in Python

This document provides a simple Python calculator program that performs basic arithmetic operations: addition, subtraction, multiplication, and division. It includes functions for each operation and handles division by zero with an error message. The user is prompted to select an operation and input two numbers, with error handling for invalid inputs.

Uploaded by

rayyanlogoexpert
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)
5 views1 page

# Simple Calculator in Python

This document provides a simple Python calculator program that performs basic arithmetic operations: addition, subtraction, multiplication, and division. It includes functions for each operation and handles division by zero with an error message. The user is prompted to select an operation and input two numbers, with error handling for invalid inputs.

Uploaded by

rayyanlogoexpert
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

# Simple Calculator in Python

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."
return x / y

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

choice = input("Enter choice (1/2/3/4):")

try:
num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))

if choice == '1':
print(f"Result: {add(num1, num2)}")
elif choice == '2':
print(f"Result: {subtract(num1, num2)}")
elif choice == '3':
print(f"Result: {multiply(num1, num2)}")
elif choice == '4':
print(f"Result: {divide(num1, num2)}")
else:
print("Invalid input")
except ValueError:
print("Please enter valid numbers.")

You might also like