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))
 
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
 

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

Popular posts from this blog

C언어) C언어를 이용하여 평균 구하기~!!

파이썬) 파이썬으로 사칙연산 계산기 만들기~!!

C언어) C언어를 이용하여 나의 BMI 측정하기(저체중, 표준체중, 과체중) 여러분도 직접 비만도를 측정해보세요~!!