Skip to content

Instantly share code, notes, and snippets.

@Vresod
Last active October 2, 2018 21:10
Show Gist options
  • Save Vresod/60d5d352c1f264a310583d014ed5419a to your computer and use it in GitHub Desktop.
Save Vresod/60d5d352c1f264a310583d014ed5419a to your computer and use it in GitHub Desktop.
calculator in python
def add(x, y):# adding
return float(x) + float(y)
def sub(x, y): #subtracting
return float(x) - float(y)
def mlt(x, y): #multiplying
return float(x) * float(y)
def div(x, y): #dividing
return float(x) / float(y)
def compare(x, y):
if x > y:
return float(x), '>', float(y)
elif x == y:
return float(x), '=', float(y)
elif x < y:
return float(x), '<', float(y)
else:
return 'Bad Input. Try Again'
print('Choose an operation:')
print('1.Addition')
print('2.Subtraction')
print('3.Multiplication')
print('4.Division')
print('5.Compare')
oper = input('Enter choice (1/2/3/4/5):')
num1 = input('Number 1?:')
num2 = input('Number 2?:')
if oper == '1':
print(num1,'+',num2,'=',add(num1, num2))
elif oper == '2':
print(num1,'-',num2,'=',sub(num1, num2))
elif oper == '3':
print(num1,'*',num2,'=',mlt(num1, num2))
elif oper == '4':
print(num1,'/',num2,'=',div(num1, num2))
elif oper == '5':
print(compare(num1, num2))
else:
print('Bad Input. Try Again')

Python Calculator

Contains 5 operations: Add, Subtract, Multiply, Divide, and Compare.

Add

Simply adds 2 numbers together.

Subtract

Simply subtracts 2 numbers together.

Multiply

Simply multiplies 2 numbers together.

Divide

Simply divides 2 numbers together.

Compare

Compares 2 numbers using >,<, and =.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment