Building Calculator – Python

How to build a calculator ?
For example, building a simple calculator
Which performs an addition, subtraction and multiplication and division

Used: if , elif , else

calculator.pngTo copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: Calculator to calculate the + – * /
”’

print
print(“This calculator can perform these operations”)
print(“Add +”)
print(“Subtract -“)
print(“Multiply *”)
print(“Divide /”)

num1 = input (‘ enter your first number : ‘)
calc1 = raw_input (‘ Select operation : ‘)
num2 = input (‘ enter your second number : ‘)

if calc1 == ‘+’:
print(str(num1)+’+’+str(num2)+’=’ +str(num1+num2))

elif calc1 == ‘-‘:
print(str(num1)+’-‘+str(num2)+’=’ +str(num1-num2))

elif calc1 == ‘*’:
print(str(num1)+’*’+str(num2)+’=’ +str(num1*num2))

elif calc1 == ‘/’:
print(str(num1)+’/’+str(num2)+’=’ +str(float(num1)/float(num2)))

else:
print(“Invalid input”)

 

calc.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s