how to make a calculator in python
#Store number variables for the two numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
#the sum of the two numbers variable
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)
#what operator to use
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
#different sums based on the operators
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))
0
0
#You can change Text at Inputs
num1 = float(input("Enter Firt Number:"))
num2 = float(input("Enter Second Number"))
result= 0
op = str(input("Enter an op(operator)"))
#Gets Input Type--------
try:
#U can make this in while loop ;-)
if op=="+":
result = num1 + num2
else if op=="-":
result = num1 - num2
else if op=="*":
result = num1 * num2
else if op=="/":
result = num1 / num2
else:
print("Invalid Input")
print("The result is " + result)
except ZeroDivisionError:
print("U cant do that")
Thank you!
0
0
3.5
2
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def multiply(num1, num2):
return num1 * num2
def divide(num1, num2):
return num1 / num2
Thank you!
2
0
3.67
3
#My Personal Python calculator
print("My Personal Python calculator")
#inputs
num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))
#opration req
opr = input('Enter the type of operation you want to perform between your chosen two numbers: ')
#calculation
if opr=='+':
ans = num1 + num2
# displaying the answer
print(f'Your final answer is {ans}')
elif opr == '- ':
ans = num1 - num2
# displaying the answer
print(f'Your final answer is {ans}')
elif opr=='*':
ans = num1 * num2;
# displaying the answer
print(f'Your final answer is {ans}')
elif opr=='/':
ans = num1 / num2
# displaying the answer
print(f'Your final answer is {ans}')
else:
print('Invalid Entry!!!')
Thank you!
3
0
3.8
5
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
Thank you!
5
0
0
6
# This will be one of the most advanced results you will find.
# We will be using classes and simple operators like +,-,*,/
class Calculator:
def addition(a,b):
return a + b
def subtraction(a,b):
if a<b:
return b - a
else:
return a - b
def multiplication(a,b):
return a * b
def division(a,b):
if a<b:
return b / a
else:
return a / b
# You can do this in terminal.
<C:/Users/username>python
>>> from main import Calculator
>>> result = Calculator.[addition|subtraction|multiplication|division](anyNumber, anyNumber)
>>> print(result)
Thank you!
6
0
Are there any code examples left?
New code examples in category Python