데이터 노가다 일지

계산기 프로젝트 본문

Python🐍

계산기 프로젝트

hoho0311 2025. 1. 20. 22:08

영~차

 

간단하고 다양한 함수들을 사용하여 간단한 계산기 프로그램을 만들어봤다.

 

함수부터 설명 시작하겠다.

def add(n1, n2):
    return n1 + n2
def sub(n1, n2):
    return n1 - n2
def mul(n1, n2):
    return n1 * n2
def div(n1, n2):
    if n2 == 0:
        return "Error! Division by zero is not allowed"
    return n1 / n2

 

워낙 간단한 함수라서 자세한 설명은 생략하겠다.

operations = {
    '+' : add,
    # add()는 함수를 사용하는것이므로 ()를 제거한다.
    '-' : sub,
    '*' : mul,
    '/' : div
}

 

연산자들이다.

키를 사용자가 입력하는 값으로 만들었고 키값은 그에 따른 연산자 함수를 불러오게 코딩하였다.

중요한 점은 ( )는 함수를 사용한다는 의미로 ( )는 생략하고 딕셔너리를 만들었다.

print(art.logo)
continue_cal = True
while continue_cal:
    number1 = float(input("What's the first number? : "))
    while True:
        operation = input(" +\n -\n *\n /\nPick an operation : ")
        if operation not in operations:
            print("You picked an invalid operation. Try again.")
            continue

        number2 = float(input("What's the second number? : "))
        result = operations[operation](number1, number2)
        print(f"Result : {result}")

        user_choice = input(f"Type 'y' continue calculation with {result}, or type 'n' to start a new calculation. : \n").lower()

        if user_choice == 'y':
            number1 = result
        else:
            break
    continue_cal = input("Do you want to perform a new calculation? (y/n) : ").lower()== 'y'
    print("\n" * 100)
print("Goodbye")

 

이 프로그램에서 가장 어려웠던 부분이다.

이제 슬슬 지피티 없으면 막힌다... 큰일난거같다.

 

근데 쉽게 해석 가능한 코드라서 적을 내용이 별로 없다.

사용자에게 number1, number2, operation을 입력받고 그에 따른 값을 출력한다.

 

그 다음 사용자에게 결과에 추가 연산의 여부를 물어본다.

'y'를 입력시 number1 = result를 해서 추가 연산을 시작한다.

 

'n' 입력시에는 다시 처음부터 값을 입력받는다.

 

마지막으로 아스키 아트 파일 올리고 글 마치겠다.

logo = r"""
 _____________________
|  _________________  |
| | Pythonista   0. | |  .----------------.  .----------------.  .----------------.  .----------------. 
| |_________________| | | .--------------. || .--------------. || .--------------. || .--------------. |
|  ___ ___ ___   ___  | | |     ______   | || |      __      | || |   _____      | || |     ______   | |
| | 7 | 8 | 9 | | + | | | |   .' ___  |  | || |     /  \     | || |  |_   _|     | || |   .' ___  |  | |
| |___|___|___| |___| | | |  / .'   \_|  | || |    / /\ \    | || |    | |       | || |  / .'   \_|  | |
| | 4 | 5 | 6 | | - | | | |  | |         | || |   / ____ \   | || |    | |   _   | || |  | |         | |
| |___|___|___| |___| | | |  \ `.___.'\  | || | _/ /    \ \_ | || |   _| |__/ |  | || |  \ `.___.'\  | |
| | 1 | 2 | 3 | | x | | | |   `._____.'  | || ||____|  |____|| || |  |________|  | || |   `._____.'  | |
| |___|___|___| |___| | | |              | || |              | || |              | || |              | |
| | . | 0 | = | | / | | | '--------------' || '--------------' || '--------------' || '--------------' |
| |___|___|___| |___| |  '----------------'  '----------------'  '----------------'  '----------------' 
|_____________________|
"""

'Python🐍' 카테고리의 다른 글

파이썬을 이용한 블랙잭 게임  (0) 2025.01.21
딕셔너리를 이용한 경매 프로그램  (0) 2025.01.19
카이사르 암호화 복호화  (2) 2025.01.19
Hang man game  (2) 2025.01.17
비밀번호 생성기  (0) 2025.01.16