프로그래밍/Python

Python 부트 캠프 - 계산기 만들기

Nowon9159 2023. 4. 30. 17:13
반응형

Python (https://towardsdatascience.com/the-zen-of-python-a-guide-to-pythons-design-principles-93f3f76d088a)

이번엔 계산기를 코딩 해봤다.

로직은 이렇다.

  1. 처음 input 값을 고르고
  2. 사칙연산 중에서 행위를 고르고
  3. 처음 input 값과 연산하기 위한 또 다른 숫자를 고른다
  4. y 값을 받으면 연산된 값으로 다시 계산하고, n 값을 받으면 계산을 종료한다.

간단한 계산기 로직이다.

# 처음 input 값으로 값 숫자를 고른다.
# 행위를 고르고 (사칙연산)
# 숫자와 연산하기위한 또 다른 숫자를 선택함
# 수식과 답을 표현 하고
# y를 타이핑 하면 계산된 답으로 다시 계산할지 n을 타이핑하면 새로운 계산을 할지 선택



first_number = input("What's the first number?: ")
value_operation = input("+\n-\n*\n/\nPick an operation: ")
next_number = input("What's the next number?: ")

check_continue = input("Type 'y' to continue calculating with {} or type 'n' to start a new claculation: ")

value_continue = False

while not value_continue :

 

처음 인풋값을 받고 계속 할지 체크하는 값을 받아 check_continue 가 y 값이면 value_continue를 변경하려고 했던 것 같다.

# 처음 input 값으로 값 숫자를 고른다.
# 행위를 고르고 (사칙연산)
# 숫자와 연산하기위한 또 다른 숫자를 선택함
# 수식과 답을 표현 하고
# y를 타이핑 하면 계산된 답으로 다시 계산할지 n을 타이핑하면 새로운 계산을 할지 선택



first_number = int(input("What's the first number?: "))
value_operation = input("+\n-\n*\n/\nPick an operation: ")
next_number = int(input("What's the next number?: "))

check_continue = input(f"Type 'y' to continue calculating with {first_number} or type 'n' to start a new claculation: ")

value_continue = False

def add(first_number, next_number):
    return first_number + next_number

def minus(first_number, next_number):
    return first_number - next_number

def multiply(first_number, next_number):
    return first_number * next_number

def divide(first_number, next_number):
    return first_number / next_number

while not value_continue :
    if value_operation == "+":
        add(first_number, next_number)
    elif value_operation == "-":
        minus(first_number, next_number)

    elif value_operation == "*":
        multiply(first_number, next_number)

    elif value_operation == "/":
        divide(first_number, next_number)
    
    if check_continue == "y":
        value_continue = False

사칙 연산을 함수로 생성하고 return 값으로 연산을 진행 했다.

while 문으로 로직을 반복하도록 했고, value_continue가 True 라면 계속 진행 되도록 했다.

그리고 check_continue 값으로 조건문을 통해 value_continue 값을 변경하려 했다.

 

# 처음 input 값으로 값 숫자를 고른다.
# 행위를 고르고 (사칙연산)
# 숫자와 연산하기위한 또 다른 숫자를 선택함
# 수식과 답을 표현 하고
# y를 타이핑 하면 계산된 답으로 다시 계산할지 n을 타이핑하면 새로운 계산을 할지 선택

value_continue = True
after_one_operation = False

def calculator(first_number, next_number, value_operation):
    if value_operation == "+":
        return first_number + next_number
    elif value_operation == "-":
        return first_number - next_number
    elif value_operation == "*":
        return first_number * next_number
    elif value_operation == "/":
        return first_number / next_number
    else :
        print("You Have a Wrong Input.")
        value_continue = False

while value_continue :
    first_number = int(input("What's the first number?: ")) # 첫째 값 받는다
    value_operation = input("+\n-\n*\n/\nPick an operation: ") # 어떤 연산을 할 지 고르기
    next_number = int(input("What's the next number?: ")) # 둘째 값 받는다

    operation_number = calculator(first_number, next_number, value_operation)

    check_continue = input(f"Type 'y' to continue calculating with {operation_number} or type 'n' to start a new claculation: ") # 연산을 계속 할 지 결정

    if check_continue == "y":
        first_number = operation_number
    elif check_continue == "n":
        break

 

굳이 저렇게 함수를 나눠 놓을 필요 없이 연산에 대해 파라미터를 받아 조건문을 한번에 처리하도록 했다.

while 반복문 로직도 input 값을 안에서 받게 하여 지속적으로 input을 받도록 했다.

또 check_continue 값을 조건문으로 체크해 y 값이면 연산이 끝난 값을 first_number 에 집어 넣어 2번째 연산 부터는 연산이 끝난 값과 연산하도록 변경 했다.

 

# 처음 input 값으로 값 숫자를 고른다.
# 행위를 고르고 (사칙연산)
# 숫자와 연산하기위한 또 다른 숫자를 선택함
# 수식과 답을 표현 하고
# y를 타이핑 하면 계산된 답으로 다시 계산할지 n을 타이핑하면 새로운 계산을 할지 선택
# y를 타이핑 한 두번째 연산 부터는 first_number의 인풋 값을 받지 않고 연산 된 값을 first_number로 대체

value_continue = True
value_after_2ndoperation = "False"
operation_number = 0

def calculator(first_number, next_number, value_operation):
    if value_operation == "+":
        return first_number + next_number
    elif value_operation == "-":
        return first_number - next_number
    elif value_operation == "*":
        return first_number * next_number
    elif value_operation == "/":
        return first_number / next_number
    else :
        print("You Put a Wrong Input.")

while value_continue :
    if value_after_2ndoperation == "True" :
        first_number = operation_number
    elif value_after_2ndoperation == "False" :
        first_number = int(input("What's the first number?: ")) # 첫째 값 받는다

    value_operation = input("+\n-\n*\n/\nPick an operation: ") # 어떤 연산을 할 지 고르기
    next_number = int(input("What's the next number?: ")) # 둘째 값 받는다
    
    operation_number = calculator(first_number, next_number, value_operation)

    check_continue = input(f"Type 'y' to continue calculating with {operation_number} or type 'n' to start a new claculation: ").lower() # 연산을 계속 할 지 결정

    if check_continue == "n":
        value_continue = False
        break
    elif check_continue == "y":
        value_continue = True
        value_after_2ndoperation = "True"

 

로직이 완벽하지 않고 y 값을 집어 넣었을 때 함수가 정상적으로 동작하지 않았다.
( y 값 입력 시 print 문만 정상적으로 동작하는 현상 발생 )

그래서 이미 기 입력된 값이 True 로 처리되는 것 같아 2ndoperation 을 새로 생성해서 2번째 연산 이후 부터는 문자열 True 를 이용해 정상적으로 동작하게끔 했다.

 

느낀점 : 강의를 안보고 혼자 로직을 짜고 디버깅을 진행 했었는데, 강의에서는 operation을 딕셔너리에 넣어서 판별했다
또한 재귀함수 호출로 함수 안에 while 문을 넣어서 끝에 특정 조건문이 충족될 때 함수를 다시 호출하게 끔 되어 있다.
강의가 요구사항이라고 가정했을 때 요구사항을 맞추지 않은 것이기 때문에 유의해야 할 것 같다.
그 외에 로직적으로 구현을 잘해서 다행이다.

 

 

반응형