Selection — IF and CASE · 선택 — IF 및 CASE
Making a decision
- Selection means choosing what to do, based on a test.
- An
ifruns its block only when the test is true. - The test gives a Boolean:
TrueorFalse.
결정하기
- 선택(selection) 은 테스트를 기반으로 무엇을 할지 선택하는 것입니다.
if블록은 테스트가 참일 때만 실행됩니다.- 테스트는 부울린(Boolean) 값을 반환합니다:
True또는False.
age = 20
if age >= 18:
print("You may vote")
else: the other path
elseruns when theiftest is false.- Exactly one of the two blocks runs.
else: 다른 경로
else은if테스트가 거짓일 때 실행됩니다.- 두 블록 중 정확히 하나만 실행됩니다.
mark = 45
if mark >= 50:
print("Pass")
else:
print("Fail")
elif: many paths
- Use
elifto test more conditions in order. - Python checks each test top to bottom and stops at the first true one.
elif: 여러 경로
- 여러 조건을 순서대로 테스트하려면
elif을 사용하세요. - Python은 각 테스트를 위에서부터 아래로檢查하고, 첫 번째 참인 것에서 멈춥니다.
mark = 72
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")
if / elif / else — which runs? · if / elif / else — 무엇이 실행될까요?
Tests run top-down; the first true one runs, the rest are skipped. · 테스트는 위에서 아래로 실행되며, 첫 번째 참인 것이 실행되고 나머지는 건너뜁니다.
Comparison operators
==means equal to (the exam writes=).!=means not equal to (the exam writes<>).- Also use
<,>,<=,>=.
비교 연산자
==은 같다(exam에서는=로 표기).!=은 다르다(exam에서는<>로 표기).- 또한
<,>,<=,>=을 사용하세요.
print(5 == 5) # True
print(5 != 3) # True
print(3 <= 2) # False
In the exam: IF and CASE
- Pseudocode uses
IF … THEN … ELSE … ENDIF, orCASEfor many fixed choices.
시험: IF와 CASE
- 가짜코드는
IF … THEN … ELSE … ENDIF을 사용하거나, 여러 고정된 선택에는CASE을 사용합니다.
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Common mistakes
- Compare with
==, not=. - Cover every case, including the
else. - Indent the block that belongs to each branch.
흔한 실수
==와 비교하고=과 비교하지 마십시오.- 모든 경우, 특히
else을 포함하여 커버하세요. - 각 분기에 속하는 블록은 들여쓰기하세요.
Now you try
- Read the input, then choose what to print with
if/elif/else. - Press Check answer to test your code.
이제 직접 해보기
- 입력을 읽고,
if/elif/else으로 어떤 것을 출력할지 선택하세요. - Answer 확인 버튼을 눌러 코드를 테스트하세요.
Read a whole number, then print even if it divides by 2 with no remainder, otherwise print odd. For input 4, print even. · 정수를 읽고, 2로 나누었을 때 나머지 없이 나면 even을 출력하고, 그렇지 않으면 odd을 출력하세요. 입력 4에 대해 even을 출력하세요.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Read a mark (0–100). Print A for 70 or more, B for 50–69, and C for anything less. For input 65, print B. · 점수(0–100)를 입력하세요. 70 이상이면 A, 50–69이면 B, 그 외의 경우 C을 출력하세요. 입력 65에 대해 B을 출력하세요.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Two numbers a and b are set for you. Use an if to store the larger one in biggest. · 두 숫자 a과 b이 주어졌습니다. if을 사용하여 더 큰 숫자를 biggest에 저장하세요.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.