calculatrice python 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 in python
from replit import clear
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1 / n2
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
def calculator():
print(logo)
num1 = float(input("What's the first number?: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
operation_symbol = input("Pick an operation: ")
num2 = float(input("What's the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: ") == 'y':
num1 = answer
else:
should_continue = False
clear()
calculator()
calculator()
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()