27 — 계산기 — 명령줄 프로젝트
3 + 4를 읽고 답한 뒤 quit을 입력할 때까지 다시 물어보는 계산기는 완성된
작은 프로젝트입니다. 한 프로그램에서 세 단계를 모두 씁니다: 입력에 초급
물어봐, 반복과 계산에 순수 Python while True:와 def, 출력에 NME
말해.
단계
계산기 전체를
calculator.ko.nme한 파일로 저장하고 실행합니다:
실행해 보기 →# 명령줄 계산기: 3 + 4 를 입력하거나 quit. # 실행: nme 실행 calculator.ko def calculate(parts): if parts[1] == "+": return int(parts[0]) + int(parts[2]) elif parts[1] == "-": return int(parts[0]) - int(parts[2]) elif parts[1] == "*": return int(parts[0]) * int(parts[2]) else: return int(parts[0]) / int(parts[2]) 말해 "계산기 — 3 + 4 같은 명령을 입력하거나 quit." while True: 물어봐 command, "명령을 입력하세요? " 만약 command == "quit": 말해 "안녕!" break parts = command.split() if len(parts) == 3: answer = calculate(parts) 말해 f"{command} = {answer}" else: 말해 "형식: 숫자 연산자 숫자"printf '3 + 4\n10 - 3\n7 * 6\n10 / 4\nquit\n' | nme 실행 calculator.ko계산기 — 3 + 4 같은 명령을 입력하거나 quit. 명령을 입력하세요? 3 + 4 = 7 명령을 입력하세요? 10 - 3 = 7 명령을 입력하세요? 7 * 6 = 42 명령을 입력하세요? 10 / 4 = 2.5 명령을 입력하세요? 안녕!계산은 함수에 들어 있습니다.
def가 이름을 붙이고,parts가 나뉜 명령을 담으며,return이 답을 돌려줍니다.parts[1]은 연산자이고int(parts[0])은 글자"3"을 숫자3으로 바꿉니다:
실행해 보기 →def calculate(parts): if parts[1] == "+": return int(parts[0]) + int(parts[2]) elif parts[1] == "-": return int(parts[0]) - int(parts[2]) elif parts[1] == "*": return int(parts[0]) * int(parts[2]) else: return int(parts[0]) / int(parts[2])elif가 연산자마다 갈래를 만들고, 마지막else가 나눗셈입니다. 이 블록은 NME 파일 안에 그대로 쓴 고급 Python입니다.split()이 명령을 단어로 자릅니다."3 + 4".split()은['3', '+', '4']가 되므로parts[0]은 첫 숫자,parts[1]은 연산자,parts[2]는 둘째 숫자입니다.len(parts) == 3은 형식에 맞지 않는 명령을 거릅니다:
실행해 보기 →parts = command.split() if len(parts) == 3: answer = calculate(parts) 말해 f"{command} = {answer}" else: 말해 "형식: 숫자 연산자 숫자"말해 f"{command} = {answer}"는 입력한 줄과 결과를 한 줄로 출력합니다.반복은 22 가이드의 메뉴와 같은 모양입니다:
while True:는 스스로 끝나지 않으므로quit이break로 나가야 합니다. 그 사이가 계산기의 한 턴입니다:
실행해 보기 →while True: 물어봐 command, "명령을 입력하세요? " 만약 command == "quit": 말해 "안녕!" break parts = command.split() if len(parts) == 3: answer = calculate(parts) 말해 f"{command} = {answer}" else: 말해 "형식: 숫자 연산자 숫자"함수가 작동하면 23 가이드처럼 함수를 자기 모듈로 옮깁니다.
calc.nme에 함수만 저장합니다:
실행해 보기 →# calc.nme — calculate 함수만 def calculate(parts): if parts[1] == "+": return int(parts[0]) + int(parts[2]) elif parts[1] == "-": return int(parts[0]) - int(parts[2]) elif parts[1] == "*": return int(parts[0]) * int(parts[2]) else: return int(parts[0]) / int(parts[2])그다음
calculator.ko.nme맨 위에서 불러옵니다. 이제 주 파일은 반복만 설명하고 계산은calc.nme에 남습니다:from "calc.nme" import calculate 말해 "계산기 — 3 + 4 같은 명령을 입력하거나 quit." while True: 물어봐 command, "명령을 입력하세요? " 만약 command == "quit": 말해 "안녕!" break parts = command.split() if len(parts) == 3: answer = calculate(parts) 말해 f"{command} = {answer}" else: 말해 "형식: 숫자 연산자 숫자"nme 검사 calculator.ko가 두 파일을 모두 확인하고,nme r calculator.ko는 전처럼 import를 실행합니다.영어 쌍둥이
calculator.nme는 같은def를 쓰고 반복을ask,if,show로 씁니다:
실행해 보기 →def calculate(parts): if parts[1] == "+": return int(parts[0]) + int(parts[2]) while True: ask command, "Your command? " if command == "quit": show "Bye!" break parts = command.split() answer = calculate(parts) show f"{command} = {answer}"같은 파이프 입력이 두 언어에서 같은 답을 냅니다.
직접 해보기
다섯 번째 연산자를 더해 보세요: else 갈래를 //(나머지 없는 나눗셈,
int(parts[0]) // int(parts[2]))로 바꾼 뒤 17 // 5를 시험하세요. 또는
모르는 연산자에 나눗셈 대신 친절한 메시지를 출력하게 하세요.
배운 것
def/return이 계산을 재사용하는 함수 하나로 묶습니다.command.split()은 줄을 토막내고int(parts[0])은 숫자를 읽습니다.while True:에quit이break로 나가면 계속 물어보는 반복이 됩니다.- 함수를
.nme모듈로 옮기면 프로젝트가 깔끔해집니다.