본문 바로가기

python study

파이썬으로 계산기 만들기

def add(n1,n2):
    return n1 + n2

def subtract(n1,n2):
    return n1 - n2

def multiply(n1,n2):
    return n1 * n2

def devide(n1,n2):
    return n1 / n2

operations = {"+":add,
              "-":subtract,
              "*":multiply,
              "/":devide,
             }

programrun = True
while programrun:
    first_number = float(input("What's the first number?: "))
    print("+")
    print("-")
    print("*")
    print("/")
    picked_operation = input("Pick an operation: ")
    next_number = float(input("What's the next number?: "))
    answer = operations[picked_operation](first_number,next_number)
    print(f"{first_number} {picked_operation} {next_number} = {answer} ")
    again = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation, or type 'q' to quit: ")
    if again == 'q':
        print("Good bye!")
        programrun = False
    if again == 'n':
        answer = 0
    elif again == 'y':
        for symbol in operations:
            new_firstnum = answer
            print("+")
            print("-")
            print("*")
            print("/")
            picked_operation = input("Pick an operation: ")
            next_number = float(input("What's the next number?: "))
            answer = operations[picked_operation](answer,next_number)
            print(f"{new_firstnum} {picked_operation} {next_number} = {answer} ")
            new_firstnum = answer
            again = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation, or type 'q' to quit: ")
            if again == 'q':
                print("Good bye!")
                programrun = False

 

왜 위에서만 new_firstnum = answer 하면 이상하게 출력되는지 모르겠다