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

Simple Calculator in Python

This Python program implements a simple calculator that performs addition and subtraction using functions. It continuously prompts the user for input until they choose to exit. The program handles invalid choices and displays the results of the calculations.

Uploaded by

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

Simple Calculator in Python

This Python program implements a simple calculator that performs addition and subtraction using functions. It continuously prompts the user for input until they choose to exit. The program handles invalid choices and displays the results of the calculations.

Uploaded by

omballare27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PHYTHON PROGRAM

# This program does addition and subtraction using functions and a loop

# Function to add two numbers


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

# Function to subtract two numbers


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

# Main program
while True:
print("\n===== Simple Calculator =====")
print("1. Add two numbers")
print("2. Subtract two numbers")
print("3. Exit")

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

if choice == '1':
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = add(num1, num2)
print("The sum is:", result)

elif choice == '2':


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = subtract(num1, num2)
print("The difference is:", result)

elif choice == '3':


print("Thank you for using the calculator!")
break
else:print("Invalid choice. Please choose 1, 2, or 3.")

You might also like