Making decisions · 做出判断
Making a choice
ifruns some code only when a condition is true.- The condition is followed by a colon
:. - The lines that belong to the
ifare indented (4 spaces).
做出选择
if只有当条件为真时才会运行某段代码。- 条件后面要加一个冒号
:。 - 属于这个
if的代码行要缩进(4 个空格)。
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
比较
- 条件通常是一个为真或为假的比较。
==等于,!=不等于,<><=>=。- 注意:
==用来判断是否相等;单个=用来存值。
temperature = 30
if temperature > 25:
print("It is hot.")
if / elif / else
elif("else if") checks another condition if the first was false.elseruns when none of the conditions were true.- Python checks them in order and stops at the first true one.
if / elif / else
- 如果第一个条件为假,
elif("else if")会再检查另一个条件。 - 当没有任何条件为真时,
else才会运行。 - Python 会按顺序检查,遇到第一个为真的就停下。
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
The remainder
a % bis what is left over whenais divided byb.year % 4 == 0means the year divides by 4 exactly — nothing left over.8 % 3is2, because 8 is two 3s with 2 left over.
余数
a % b是a除以b剩下的数。year % 4 == 0表示年份正好被 4 整除 —— 没有余数。8 % 3是2,因为 8 是两个 3,还剩 2。
Combine conditions
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
组合条件
and:只有两边都为真时才为真。or:至少有一边为真时就为真。not:把真变假,把假变真。
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
用剑桥伪代码表示
- Python 的
if/elif/else对应考试里的IF … ELSE … ENDIF和CASE。
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
CASE OF Grade
"A": OUTPUT "Top"
"B": OUTPUT "Good"
OTHERWISE OUTPUT "Keep trying"
ENDCASE
Common mistakes
- Compare with
==; a single=assigns. - Write
elif, notelse if, and end eachif/elif/elseline with:. - The indented block below
ifis what runs when the condition is true.
常见错误
- 比较用
==;单个=是赋值。 - 要写
elif,不是else if,每个if/elif/else行都以:结尾。 if下面缩进的语句块,是条件为真时运行的部分。
Now you try
- When you Run, type the input in the box under the code and press Enter.
- The checks type the input for you. Read it, then decide what to print.
- Match the output text exactly.
现在轮到你
- 按运行时,在代码下面的方框里输入,再按回车键。
- 检查会替你输入数据。先读取,再决定要打印什么。
- 输出文字要完全一致。
if / elif / else
Conditions are checked top-down; only the first true branch runs. · 条件从上往下检查;只有第一个为真的分支会运行。
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass. · 用 int(input()) 读取一个分数。如果它大于或等于 50,打印 Pass,否则打印 Fail。当输入为 72 时,打印 Pass。
Click Run to see the output here. · 点击“运行”查看此处输出。
Read a score. Print A if it is 80+, B if it is 60+, otherwise C. Use if / elif / else. For input 75, print B. · 读取一个分数。如果是 80 及以上打印 A,60 及以上打印 B,否则打印 C。使用 if / elif / else。当输入为 75 时,打印 B。
Click Run to see the output here. · 点击“运行”查看此处输出。
Read an age. Print Yes if the age is from 13 to · 到 19 (a teenager), otherwise No. Use and. For input 16, print Yes. · 读取一个年龄。如果年龄在 13 到 19 之间(青少年),打印 Yes,否则打印 No。使用 and。当输入为 16 时,打印 Yes。
Click Run to see the output here. · 点击“运行”查看此处输出。
Read a year with int(input()). A year divides by 4 when the remainder is 0: year % 4 == 0. Print Leap if it divides by 4, otherwise Common. For the input 2024, print Leap. · 用 int(input()) 读取一个年份。余数是 0 时,年份能被 4 整除:year % 4 == 0。能被 4 整除就打印 Leap,否则打印 Common。输入 2024 时,打印 Leap。
Click Run to see the output here. · 点击“运行”查看此处输出。
Dividing by 4 is not the whole rule. A year that also divides by 100 is not · 不 a leap year, unless it divides by 400 too. 1900 is the trap: it divides by 4 and by 100, so print Common. Print Leap or · 或 Common. · 能被 4 整除还不是全部规则。还能被 100 整除的年份不是闰年,除非它也能被 400 整除。1900 就是这个陷阱:它能被 4 整除,也能被 100 整除,所以打印 Common。打印 Leap 或 Common。
Click Run to see the output here. · 点击“运行”查看此处输出。