how to make python calculator code example
Example 1: simple python calculator
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
if choice == '+':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
if choice == '-':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum2))
if choice == '*':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum3))
if choice == '/':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum4))
Example 2: calculator in python
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15.0 * 14.0 = 210.0
Example 3: python calculator
try:
num1 = float(input("Please Enter The first number: "))
operator = input("Please choose the operator (+) for addition, (-) for subtraction, (*) for multiplication"
"(/) for division , (%) for remainder: ")
num2 = float(input("Please Enter The second number: "))
if operator == '+':
result = num1 + num2
print("The result is: ", result)
elif operator == '-':
result = num1 - num2
print("The result is: ", result)
elif operator == '*':
result = num1 * num2
print("The result is: ", result)
elif operator == '/':
try:
if True:
result = num1 / num2
print(result)
except ZeroDivisionError as err:
print(err, " oops! zero division occur ")
elif operator == '%':
if operator == '%':
result = num1 % num2
print("The result is: ", result)
else:
raise TypeError
except ValueError:
print("wrong value, suggest integer or decimal")
finally:
print("All Done")
Example 4: python calculator
print("Choose operator (+,-,*,/):")
mode = input()
print("Choose first int:")
x = int(input())
print("Choose second int:")
y = int(input())
print("Your result:")
if mode == "+":
print(x+y)
elif mode == "-":
print(x-y)
elif mode == "*":
print(x*y)
elif mode == "/":
print(x/y)