python) Let's make calculator using python~~
Hi guys~~
I'm lulu~ :-)
Today, I'm going to make two calculators using python.
First is a calculator that calculates plus, minus, multiple and division if a user input two numbers.
Second calculator will only show one result when a user input two numbers and a operation.
These days python is really loved so studying python will be helpful.
It's not hard to make it. Let's see how to make calculator.
First calculator)
print("Input two numbers:") #first, I printed "Input two numbers"
x=int(input('')) # after that, I stored two numbers at "x" and "y" each.
y=int(input(''))
def my_plus(x,y): # I used "def" function to make my_plus function
return x+y #my_plus function will return "x+y"
print("Plus result is", my_plus(x,y)) #and finally my_plus will print "Plus result is x+y"
def my_minus(x,y): #again, I made my_minus . Same structure with my_plus.
return x-y #but it returns "x-y"
print("Minus result is", my_minus(x,y))
def my_multiply(x,y): #I made my_multiply function. Also same structure with above functions.
return x*y
print("Multiply result is", my_multiply(x,y))
def my_division(x,y):
return x/y
print("Division result is", my_division(x,y))
x=int(input('')) # after that, I stored two numbers at "x" and "y" each.
y=int(input(''))
def my_plus(x,y): # I used "def" function to make my_plus function
return x+y #my_plus function will return "x+y"
print("Plus result is", my_plus(x,y)) #and finally my_plus will print "Plus result is x+y"
def my_minus(x,y): #again, I made my_minus . Same structure with my_plus.
return x-y #but it returns "x-y"
print("Minus result is", my_minus(x,y))
def my_multiply(x,y): #I made my_multiply function. Also same structure with above functions.
return x*y
print("Multiply result is", my_multiply(x,y))
def my_division(x,y):
return x/y
print("Division result is", my_division(x,y))
This way, I made calculator that will print plus, minus, multiple and divise two numbers that user input.
Next, I will make a calculator that will input two numbers and operation!!
Then, this calculator will calculate two numbers with input operation.
Second calculator)
print("Input two numbers:") #First, we will print "input two numbers"
x=int(input('')) #and then, store that two numbers at 'x' and 'y'
y=int(input(''))
op=input('Input operation:') #This calculator will be input operation by a user.
def my_plus(x,y):
return x+y
def my_minus(x,y):
return x-y
def my_multiply(x,y):
return x*y
def my_division(x,y):
return x/y -----------> This is same with first calculator but it doesn't have to print each function. So we don't have to write "print".
def my_calculate(x,y,op):
if op == '+' :
return my_plus(x,y)
if op == '-':
return my_minus(x,y)
if op == '*':
return my_multiply(x,y)
if op == '/':
return my_division(x,y) ----------> If a user puts '+', then this calculator will plus two numbers. If a user puts '-' this calculator will minus two numbers. If a user puts '*' at operation, then this calculaor will multiply two numbers. If a user puts '/', then it will divise two numbers.
else:
print("Sorry, check your inputs") --------> If a user puts other operations except abover four operations( +,-,*,/), then I will print "Sorry, check your inputs"
print("Result is", my_calculate(x,y,op) ) -------> Lastly, we will print"Result is" and then execute my_calculate function
x=int(input('')) #and then, store that two numbers at 'x' and 'y'
y=int(input(''))
op=input('Input operation:') #This calculator will be input operation by a user.
def my_plus(x,y):
return x+y
def my_minus(x,y):
return x-y
def my_multiply(x,y):
return x*y
def my_division(x,y):
return x/y -----------> This is same with first calculator but it doesn't have to print each function. So we don't have to write "print".
def my_calculate(x,y,op):
if op == '+' :
return my_plus(x,y)
if op == '-':
return my_minus(x,y)
if op == '*':
return my_multiply(x,y)
if op == '/':
return my_division(x,y) ----------> If a user puts '+', then this calculator will plus two numbers. If a user puts '-' this calculator will minus two numbers. If a user puts '*' at operation, then this calculaor will multiply two numbers. If a user puts '/', then it will divise two numbers.
else:
print("Sorry, check your inputs") --------> If a user puts other operations except abover four operations( +,-,*,/), then I will print "Sorry, check your inputs"
print("Result is", my_calculate(x,y,op) ) -------> Lastly, we will print"Result is" and then execute my_calculate function
I intended to input other operation~ You can easily make this calculator~!!
If you have any questions or any opinions, tell me you'll be welcomed :-)
Thank you
Comments
Post a Comment