티스토리 뷰

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

 

 

'Test > Python(20220101~)' 카테고리의 다른 글

Days11_Black Jack, 21  (0) 2022.01.16
Python 화면 지우기  (0) 2022.01.15
Days10_Docstrings in Functions  (0) 2022.01.15
Days10_Functions with Outputs  (0) 2022.01.14
Days9_**비밀경매 프로그램  (0) 2022.01.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함