python calculator library code example
Example 1: python calculator
print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
num = int(input())
if num == 1:
print("Enter Number 1 : ")
add1 = int(input())
print("Enter Number 2 : ")
add2 = int(input())
sum = add1 + add2
print("The Sum Is ", sum)
elif num == 2:
print("Enter Number 1 : ")
sub1 = int(input())
print("Enter Number 2 : ")
sub2 = int(input())
difference = sub1 - sub2
print("The Difference Is ", difference)
elif num == 3:
print("Enter Number 1 : ")
div1 = float(input())
print("Enter Number 2 : ")
div2 = float(input())
division = div1 / div2
print("The Division Is ", division)
elif num == 4:
print("Enter Number 1 : ")
mul1 = int(input())
print("Enter Number 2 : ")
mul2 = int(input())
multiply = mul1 * mul2
print("The Difference Is ", multiply)
else:
print("enter a valid Number")
Example 2: calculator python
def fractions_regular(fractions):
f1 = input(13,25)
f1 = fractions.Fraction(f1)
f2 = input(6,25)
f2 = fractions.Fraction(f2)
operation = input('what is your iperation?')
if operation == '+':
print('{} + {} = {}'.format(f1, f2, f1 + f2))
if operation == "-":
print('{} - {} = {}'.format(f1, f2, f1 - f2))
if operation == "*":
print('{} * {} = {}'.format(f1, f2, f1 * f2))
if operation == "/":
print('{} / {} = {}'.format(f1, f2, f1 / f2))
Example 3: python calculator
import time
def cal():
def add(x, y):
print(x + y)
def sub(x, y):
print(x - y)
def mul(x, y):
print(x * y)
def div(x, y):
print(x // y)
while True:
print('Adding?(+) Subtracting?(-) Multiplying?(*) Dividing?(/)')
answer = input('')
if answer == "+":
try:
x = float(input('First number! '))
y = float(input('Second Number! '))
except Exception as e:
print(f'Error: {e}')
time.sleep(5)
return
add(x, y)
elif answer == "-":
try:
x = float(input('First number! '))
y = float(input('Second Number! '))
except Exception as e:
print(f'Error: {e}')
time.sleep(5)
return
sub(x, y)
elif answer == "*":
try:
x = float(input('First number! '))
y = float(input('Second Number! '))
except Exception as e:
print(f'Error: {e}')
time.sleep(5)
return
mul(x, y)
elif answer == "/":
try:
x = float(input('First number! '))
y = float(input('Second Number! '))
except Exception as e:
print(f'Error: {e}')
time.sleep(5)
return
div(x, y)
else:
print('Error. Enter a valid input!')
cal()