0% found this document useful (0 votes)
2 views

python codes

The document contains two Python calculator programs: a basic calculator and a complex calculator. The basic calculator performs addition, subtraction, multiplication, and division, while the complex calculator adds exponentiation, square root, and modulus operations. Both calculators allow users to perform multiple calculations in a loop until they choose to exit.

Uploaded by

chaitanya45506
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python codes

The document contains two Python calculator programs: a basic calculator and a complex calculator. The basic calculator performs addition, subtraction, multiplication, and division, while the complex calculator adds exponentiation, square root, and modulus operations. Both calculators allow users to perform multiple calculations in a loop until they choose to exit.

Uploaded by

chaitanya45506
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# basic calculator

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 "Cannot divide by zero!"

return x / y

print("Select operation:")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")

while True:

# Take input from the user

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

if choice in ('1', '2', '3', '4'):

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

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)}")

# check if user wants another calculation

# break the while loop if answer is no

next_calculation = input("Let's do next calculation? (yes/no): ")

if next_calculation.lower() != 'yes':

break

else:

print("Invalid Input")
# complex calculator

import math

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 "Cannot divide by zero!"

return x / y

def exponent(x, y):

return x ** y

def square_root(x):

if x < 0:

return "Cannot take the square root of a negative number!"

return math.sqrt(x)

def modulus(x, y):

return x % y

print("Select operation:")

print("1. Add")

print("2. Subtract")
print("3. Multiply")

print("4. Divide")

print("5. Exponentiation (x^y)")

print("6. Square Root")

print("7. Modulus (x % y)")

while True:

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

if choice in ('1', '2', '3', '4', '5', '6', '7'):

if choice == '6':

num = float(input("Enter the number: "))

print(f"Square root of {num} = {square_root(num)}")

else:

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

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)}")

elif choice == '5':

print(f"{num1} ^ {num2} = {exponent(num1, num2)}")


elif choice == '7':

print(f"{num1} % {num2} = {modulus(num1, num2)}")

next_calculation = input("Let's do the next calculation? (yes/no): ")

if next_calculation.lower() != 'yes':

break

else:

print("Invalid Input")

You might also like