how to make a calculator using 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))
4.22
9
#no gui
def add(a,b):
result=a+b
print(result)
def sub(a,b):
resulf=a-b
print(resulf)
def mul(a,b):
resulf=a*b
print(resulf)
def div(a,b):
resulf=a/b
print(resulf)
a=int(input("enter th first number: "))
b=int(input("enter th second number: "))
op=input("enter the sign: ")
if op=="+":
add(a,b)
elif op=="-":
sub(a,b)
elif op=="*":
mul(a,b)
elif op=="/":
div(a,b)
else:
print("invalid")
Thank you!
9
0
3.83
6
#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!
6
0
3.5
6
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!
6
0
4.5
2
# Define variables for number inputs..
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Input for operators
op = input("Enter operator: ")
# Functions for operations
def add(num1, num2):
print('Result: ', num1 + num2)
def subtract(num1, num2):
print('Result: ', num1 - num2)
def multiply(num1, num2):
print('Result: ', num1 * num2)
def division(num1, num2):
print('Result: ', num1 / num2)
# Using the functions
if op == '+':
add(num1=num1, num2=num2)
elif op == '-':
subtract(num1=num1, num2=num2)
elif op == '*':
multiply(num1=num1, num2=num2)
elif op == '/':
division(num1=num1, num2=num2)
else:
print('Invalid operator')
Thank you!
2
0
3.9
10
#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!
10
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types