Test/Python(20220101~)
Days10_계산기 최종
kiostory
2022. 1. 15. 13:04
from art import logo
def add(n1, n2):
return n1+n2
def substract(n1, n2):
return n1-n2
def multiply(n1, n2):
return n1*n2
def divide(n1, n2):
return n1/n2
#함수를 정의하고 함수를 딕셔너리에 결합 : key는 산술부호, value는 각 함수명
calculate = {
"+": add,
"-": substract,
"*": multiply,
"/": divide,
}
def calculator():
import os
if os.name in ('nt', 'dos'): # If Machine is running on Windows, use cls
os.system('cls')
else:
os.system('clear')
print(logo)
#function = calculate["*"]
#function(2,3) ## 이거슨 곱하기 함수가 될 꺼임.....
repeat = True
num1 = float(input("What's the first number? : "))
while repeat:
for i in calculate:
print(i)
operate = input("Pick an operation : ")
num2 = float(input("What's the next number? : "))
## 핵심임
function = calculate[operate]
output = function(num1, num2) ## 이거슨 산술하는 함수가 될 꺼임.....
## 핵심임
print(f"{num1} {operate} {num2} = {output}")
answer = input(f"Type 'y' to continue calculating with the result {output}, \nor type 'n' to start a new calculation: ").lower()
if answer == "n":
repeat = False
calculator()
elif answer == "y":
num1 = output
else:
print("Invalid input!! Quit.")
repeat = False
calculator()