Skip to content

Commit d6989a0

Browse files
committed
user input
1 parent 36ed850 commit d6989a0

File tree

1 file changed

+91
-97
lines changed

1 file changed

+91
-97
lines changed

book_kr/book.tex

Lines changed: 91 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,7 +3557,7 @@ \section{불 표현식}
35573557
%
35583558
이 연산자들이 익숙해 보일 수 있지만, Python에서 사용하는 기호가 수학
35593559
기호와 다르다. 가장 흔한 실수는 하나의 등호({\tt =})를 써서 두 값이
3560-
같은지 비교((\tt ==})하는 것이다. {\tt =}는 할당 연산자이고 {\tt ==}가
3560+
같은지 비교({\tt ==})하는 것이다. {\tt =}는 할당 연산자이고 {\tt ==}가
35613561
관계 연산자인 것을 기억해야 한다. 그리고 {\tt =<}나 {\tt =>}와 같은
35623562
연산자는 없다.
35633563

@@ -3640,7 +3640,7 @@ \section{조건부 실행}
36403640

36413641
\begin{verbatim}
36423642
if x < 0:
3643-
pass # TODO: need to handle negative values!
3643+
pass # TODO: 음수 처리해야 함!
36443644
\end{verbatim}
36453645
%
36463646

@@ -3657,9 +3657,9 @@ \section{선택적 실행}
36573657

36583658
\begin{verbatim}
36593659
if x % 2 == 0:
3660-
print('x is even')
3660+
print('x는 짝수')
36613661
else:
3662-
print('x is odd')
3662+
print('x는 음수')
36633663
\end{verbatim}
36643664
%
36653665
{\tt x}를 2로 나눈 나머지가 0이라면 {\tt x}가 짝수이고, 프로그램은
@@ -3864,70 +3864,70 @@ \section{재귀문}
38643864
\index{loop!for}
38653865

38663866

3867-
\section{Stack diagrams for recursive functions}
3867+
\section{재귀 함수의 스택 상태도}
3868+
%Stack diagrams for recursive functions
38683869
\label{recursive.stack}
38693870
\index{stack diagram}
38703871
\index{function frame}
38713872
\index{frame}
38723873

3873-
In Section~\ref{stackdiagram}, we used a stack diagram to represent
3874-
the state of a program during a function call. The same kind of
3875-
diagram can help interpret a recursive function.
3874+
\ref{stackdiagram}절에서 함수 호출 중에 프로그램의 상태를 스택 상태도로
3875+
표현하였었다. 똑같은 그림을 재귀문을 이해하는데도 쓸 수 있다.
38763876

3877-
Every time a function gets called, Python creates a
3878-
frame to contain the function's local variables and parameters.
3879-
For a recursive function, there might be more than one frame on the
3880-
stack at the same time.
3877+
함수가 호출될 때마다 Python은 프레임을 생성하여 함수의 지역 변수와 매개
3878+
변수를 저장한다. 재귀문을 쓰게 되면 스택에 하나 이상의 프레임이
3879+
동시에 여러 개가 존재할 수 있다.
38813880

3882-
Figure~\ref{fig.stack2} shows a stack diagram for {\tt countdown} called with
3883-
{\tt n = 3}.
3881+
그림~\ref{fig.stack2}은 {\tt countdown}을 {\tt n = 3}을 인자로 하여
3882+
호출했을 때의 스택 상태도를 나타낸다.
38843883

38853884
\begin{figure}
38863885
\centerline
38873886
{\includegraphics[scale=0.8]{figs/stack2.pdf}}
3888-
\caption{Stack diagram.}
3887+
\caption{스택 상태도.}
38893888
\label{fig.stack2}
38903889
\end{figure}
38913890

3892-
3893-
As usual, the top of the stack is the frame for \verb"__main__".
3894-
It is empty because we did not create any variables in
3895-
\verb"__main__" or pass any arguments to it.
3891+
스택의 최상위 프레임에 \verb"__main__"가 있다. 프레임에 아무 것도 없는
3892+
이유는 \verb"__main__"가 어떤 변수도 생성하지 않았고 인자도 전달받지
3893+
못했기 때문이다.
38963894
\index{base case}
38973895
\index{recursion!base case}
38983896

3899-
The four {\tt countdown} frames have different values for the
3900-
parameter {\tt n}. The bottom of the stack, where {\tt n=0}, is
3901-
called the {\bf base case}. It does not make a recursive call, so
3902-
there are no more frames.
3897+
네 개의 {\tt countdown} 프레임들은 매개 변수 {\tt n}이 모두 다르다.
3898+
{\tt n = 0}이 있는 스택 프레임을 {\bf 기준 케이스(base case)}라
3899+
부른다. 더 이상 재귀 호출을 하지 않기 때문에 그 이하로는 프레임이
3900+
없다.
39033901

3904-
As an exercise, draw a stack diagram for \verb"print_n" called with
3905-
\verb"s = 'Hello'" and {\tt n=2}.
3906-
Then write a function called \verb"do_n" that takes a function
3907-
object and a number, {\tt n}, as arguments, and that calls
3908-
the given function {\tt n} times.
39093902

3903+
연습삼아 \verb"s = 'Hello'"와 {\tt n=2}를 인자로 \verb"print_n"가
3904+
호출되었을 때의 스택 상태도를 그려보자. 그리고 함수 객체와 {\tt n}을
3905+
인자로 하는 \verb"do_n" 함수를 만들어서 그 함수를 {\tt n}번 호출해
3906+
보자.
39103907

3911-
\section{Infinite recursion}
3908+
3909+
\section{무한 재귀문}
3910+
%Infinite recursion
39123911
\index{infinite recursion}
39133912
\index{recursion!infinite}
39143913
\index{runtime error}
39153914
\index{error!runtime}
39163915
\index{traceback}
39173916

3918-
If a recursion never reaches a base case, it goes on making
3919-
recursive calls forever, and the program never terminates. This is
3920-
known as {\bf infinite recursion}, and it is generally not
3921-
a good idea. Here is a minimal program with an infinite recursion:
3917+
재귀문이 기준 케이스에 도달하지 못한다면 재귀문은 끝없이 호출되고
3918+
프로그램은 절대로 종료하지 않는다. 이것이 {\bf 무한 재귀문(infinite
3919+
recursion)}이고 일반적으로 나쁘다. 무한 재귀문의 한 짧은
3920+
예를 살펴보자.
3921+
39223922

39233923
\begin{verbatim}
39243924
def recurse():
39253925
recurse()
39263926
\end{verbatim}
39273927
%
3928-
In most programming environments, a program with infinite recursion
3929-
does not really run forever. Python reports an error
3930-
message when the maximum recursion depth is reached:
3928+
대부분의 프로그래밍 환경에서 무한 재귀문을 갖는 프로그램이 무한히
3929+
동작하는 일은 없다. 프로그램이 최대 재귀 가능 횟수에 도달하면 Python은
3930+
오류 메시지를 보고 한다.
39313931
\index{exception!RuntimeError}
39323932
\index{RuntimeError}
39333933

@@ -3942,28 +3942,28 @@ \section{Infinite recursion}
39423942
RuntimeError: Maximum recursion depth exceeded
39433943
\end{verbatim}
39443944
%
3945-
This traceback is a little bigger than the one we saw in the
3946-
previous chapter. When the error occurs, there are 1000
3947-
{\tt recurse} frames on the stack!
3945+
이 트레이스백은 이 전 장에서 살펴보았던 보다 좀 더 길다. 오류가
3946+
발생하면서 스택에 1,000개의 {\tt recurse} 프레임이 생겼다.
3947+
3948+
실수로 무한 재귀문을 만나게 되면 재귀 호출에 기준 케이스가 있는지
3949+
함수를 확인해야 한다. 기준 케이스가 있다면 그 부분에 도달 가능한지
3950+
확인해야 한다.
39483951

3949-
If you encounter an infinite recursion by accident, review
3950-
your function to confirm that there is a base case that does not
3951-
make a recursive call. And if there is a base case, check whether
3952-
you are guaranteed to reach it.
39533952

39543953

3955-
\section{Keyboard input}
3954+
\section{키보드 입력}
3955+
%Keyboard input
39563956
\index{keyboard input}
39573957

3958-
The programs we have written so far accept no input from the user.
3959-
They just do the same thing every time.
3958+
지금까지 작성한 모든 프로그램은 사용자 입력이 없었다. 실행하면
3959+
똑같은 동작만 했다.
3960+
3961+
Python은 {\tt input}이라는 내장 함수를 제공한다. 이 함수는 잠시 멈춰서
3962+
사용자가 입력하기를 기다린다. 사용자가 {\sf Return} 또는 {\sf
3963+
Enter}키를 누르면 프로그램은 계속 동작하고 \verb"input"은 사용자가
3964+
입력한 문자열을 리턴한다. Python 2에서는 똑같은 일을 하는 함수를
3965+
\verb"raw_input"이라 부른다.
39603966

3961-
Python provides a built-in function called {\tt input} that
3962-
stops the program and
3963-
waits for the user to type something. When the user presses {\sf
3964-
Return} or {\sf Enter}, the program resumes and \verb"input"
3965-
returns what the user typed as a string. In Python 2, the same
3966-
function is called \verb"raw_input".
39673967
\index{Python 2}
39683968
\index{input function}
39693969
\index{function!input}
@@ -3975,71 +3975,69 @@ \section{Keyboard input}
39753975
'What are you waiting for?'
39763976
\end{verbatim}
39773977
%
3978-
Before getting input from the user, it is a good idea to print a
3979-
prompt telling the user what to type. \verb"input" can take a
3980-
prompt as an argument:
3978+
사용자로 부터 입력을 받기 전에 어떤 것을 입력할지 알려주는 것이 좋다.
3979+
\verb"input"는 인자로 프롬프트의 내용을 갖는다.
39813980
\index{prompt}
39823981

39833982
\begin{verbatim}
3984-
>>> name = input('What...is your name?\n')
3985-
What...is your name?
3986-
Arthur, King of the Britons!
3983+
>>> name = input('당신은 누구죠?\n')
3984+
당신은 누구죠?
3985+
영국의 왕, 아서!
39873986
>>> name
3988-
'Arthur, King of the Britons!'
3987+
'영국의 왕, 아서!'
39893988
\end{verbatim}
39903989
%
3991-
The sequence \verb"\n" at the end of the prompt represents a {\bf
3992-
newline}, which is a special character that causes a line break.
3993-
That's why the user's input appears below the prompt. \index{newline}
3990+
프롬프트의 마지막 부분의 \verb"\n"{\bf 새 줄(newline)}을 나타내는
3991+
특수 기호로 줄을 바꾼다. 그렇기 때문에 사용자 입력 위치가 프롬프트 아래에 있다.
3992+
\index{newline}
39943993

3995-
If you expect the user to type an integer, you can try to convert
3996-
the return value to {\tt int}:
3994+
정수를 입력하기를 바란다면 리턴 값을 {\tt int}으로 변환할 수 있다.
39973995

39983996
\begin{verbatim}
3999-
>>> prompt = 'What...is the airspeed velocity of an unladen swallow?\n'
3997+
>>> prompt = '짐을 메달고 있지 않은 제비의 속도는?\n'
40003998
>>> speed = input(prompt)
4001-
What...is the airspeed velocity of an unladen swallow?
3999+
짐을 메달고 있지 않은 제비의 속도는?
40024000
42
40034001
>>> int(speed)
40044002
42
40054003
\end{verbatim}
40064004
%
4007-
But if the user types something other than a string of digits,
4008-
you get an error:
4005+
하지만 사용자가 숫자 말고 다른 것을 입력하면 오류가 발생한다.
40094006

40104007
\begin{verbatim}
40114008
>>> speed = input(prompt)
4012-
What...is the airspeed velocity of an unladen swallow?
4013-
What do you mean, an African or a European swallow?
4009+
짐을 메달고 있지 않은 제비의 속도는?
4010+
그 제비가 아프리카 제비야 아니면 영국 제비야?
40144011
>>> int(speed)
40154012
ValueError: invalid literal for int() with base 10
40164013
\end{verbatim}
40174014
%
4018-
We will see how to handle this kind of error later.
4015+
이런 류의 오류를 다루는 법을 나중에 보도록 하겠다.
40194016
\index{ValueError}
40204017
\index{exception!ValueError}
40214018

40224019

4023-
\section{Debugging}
4020+
\section{디버깅}
4021+
%Debugging
40244022
\label{whitespace}
40254023
\index{debugging}
40264024
\index{traceback}
40274025

4028-
When a syntax or runtime error occurs, the error message contains
4029-
a lot of information, but it can be overwhelming. The most
4030-
useful parts are usually:
4026+
문법 오류나 실행 중에 발생하는 오류에는 많은 정보가 담겨 있다. 때로는
4027+
벅차게 많을 수도 있다. 그 중에 중요한 정보들은 다음과 같다.
4028+
40314029

40324030
\begin{itemize}
40334031

4034-
\item What kind of error it was, and
4032+
\item 어떤 종류의 오류인가
40354033

4036-
\item Where it occurred.
4034+
\item 어디서 발생했는가
40374035

40384036
\end{itemize}
40394037

4040-
Syntax errors are usually easy to find, but there are a few
4041-
gotchas. Whitespace errors can be tricky because spaces and
4042-
tabs are invisible and we are used to ignoring them.
4038+
문법 오류는 대체적으로 찾기가 쉽지만 몇 개는 눈에 잘 안 띄기도 한다.
4039+
사이띄기와 탭 문자 같은 공백과 관련한 오류들은 보이지 않기 때문에 쉽게
4040+
지나치곤 한다. 그래서 찾기가 쉽지 않다.
40434041
\index{whitespace}
40444042

40454043
\begin{verbatim}
@@ -4051,18 +4049,15 @@ \section{Debugging}
40514049
IndentationError: unexpected indent
40524050
\end{verbatim}
40534051
%
4054-
In this example, the problem is that the second line is indented by
4055-
one space. But the error message points to {\tt y}, which is
4056-
misleading. In general, error messages indicate where the problem was
4057-
discovered, but the actual error might be earlier in the code,
4058-
sometimes on a previous line.
4052+
이 예제에서의 문제는 두 번째 줄이 한 칸 들여쓰기되어 있다는 것이다.
4053+
그렇지만 오류 메시지는 {\tt y}를 가리키고 있기 때문에 오해하기 쉽다.
4054+
일반적으로 오류 메시지는 오류가 발견된 시점을 나타내기 때문에 실제
4055+
오류는 코드의 이전에서 이미 있었을 수도 있다. 심지어는 전 줄에 오류가
4056+
있었을 수도 있다.
40594057
\index{error!runtime}
40604058
\index{runtime error}
40614059

4062-
The same is true of runtime errors. Suppose you are trying
4063-
to compute a signal-to-noise ratio in decibels. The formula
4064-
is $SNR_{db} = 10 \log_{10} (P_{signal} / P_{noise})$. In Python,
4065-
you might write something like this:
4060+
실행 중 발생 오류에서도 마찬가지다. 신호대잡음비를 데시벨로 계산한다고 해보자. 계산식은 $SNR_{db} = 10 \log_{10} (P_{signal} / P_{noise})$이다. Python에서는 이 식을 작성하면 다음과 같다.
40664061

40674062
\begin{verbatim}
40684063
import math
@@ -4073,7 +4068,7 @@ \section{Debugging}
40734068
print(decibels)
40744069
\end{verbatim}
40754070
%
4076-
When you run this program, you get an exception:
4071+
이 프로그램을 실행하면 예외처리가 된다.
40774072
%
40784073
\index{exception!OverflowError}
40794074
\index{OverflowError}
@@ -4085,16 +4080,15 @@ \section{Debugging}
40854080
ValueError: math domain error
40864081
\end{verbatim}
40874082
%
4088-
The error message indicates line 5, but there is nothing
4089-
wrong with that line. To find the real error, it might be
4090-
useful to print the value of {\tt ratio}, which turns out to
4091-
be 0. The problem is in line 4, which uses floor division
4092-
instead of floating-point division.
4083+
이 오류 메시지는 5번 줄을 가리키고 있지만 그 줄에는 문제가 없다. 실제
4084+
오류를 찾으려면 {\tt ratio}의 값을 출력해보는게 좋다. 출력해보면 0
4085+
값을 리턴 받는다. 실제 문제는 내림 나눗셈을 하는 4번 줄에 있다.
4086+
소수점 단위의 나눗셈을 했어야 했다.
40934087
\index{floor division}
40944088
\index{division!floor}
40954089

4096-
You should take the time to read error messages carefully, but don't
4097-
assume that everything they say is correct.
4090+
오류 메시지를 읽는데 충분한 시간을 들여야 하겠지만, 그 하는 말을 모두
4091+
참이라 믿어서는 안된다.
40984092

40994093

41004094
\section{용어 해설}

0 commit comments

Comments
 (0)