Making decisions · Ra quyết định
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).
Tạo sự lựa chọn
ifchạy mã chỉ khi một điều kiện đúng.- Điều kiện theo sau là dấu hai chấm
:. - Các dòng thuộc về
ifsẽ được thu vào trong (4 khoảng trắng).
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
So sánh
- Một điều kiện thường là một phép so sánh mang giá trị Đúng hoặc Sai.
==bằng nhau,!=không bằng nhau,<><=>=.- Lưu ý:
==kiểm tra sự bằng nhau; một=đơn lẻ gán một giá trị.
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") kiểm tra một điều kiện khác nếu điều kiện đầu tiên sai.elsechạy khi không có điều kiện nào đúng.- Python kiểm tra chúng theo thứ tự và dừng lại ở điều kiện đúng đầu tiên.
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.
Phần dư
a % blà phần còn lại khiachia chob.year % 4 == 0có nghĩa là năm chia hết cho 4 — không còn phần dư.8 % 3là2, vì 8 là hai số 3 cộng thêm 2 phần dư.
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.
Kết hợp các điều kiện
andchỉ đúng khi cả hai vế đều đúng.orđúng khi ít nhất một vế đúng.notđảo ngược true sang false và false sang true.
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
Trong pseudocode Cambridge
- Python
if/elif/elsekhớp với examIF … ELSE … ENDIFvà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.
Lỗi thường gặp
- So sánh với
==; một=đơn lẻ gán giá trị. - Viết
elif, không viếtelse if, và kết thúc mỗi dòngif/elif/elsevới:. - Khối code thụt vào bên dưới
iflà những gì sẽ chạy khi điều kiện đúng.
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.
Bây giờ bạn thử
- Khi bạn Chạy, hãy nhập đầu vào vào ô bên dưới code và nhấn Enter.
- Các bước kiểm tra sẽ tự động nhập đầu vào cho bạn. Hãy đọc nó, sau đó quyết định phải in gì.
- Khớp chính xác với văn bản đầu ra.
if / elif / else
Conditions are checked top-down; only the first true branch runs. · Các điều kiện được kiểm tra từ trên xuống dưới; chỉ có nhánh đầu tiên đúng được thực thi.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass. · Đọc điểm số với int(input()). In Pass nếu điểm đó là 50 hoặc hơn, ngược lại in Fail. Với đầu vào 72, in ra Pass.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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. · Đọc điểm số. In A nếu nó là 80+, B nếu nó là 60+, ngược lại là C. Sử dụng if / elif / else. Với đầu vào 75, in ra B.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Read an age. Print Yes if the age is from 13 to 19 (a teenager), otherwise No. Use and. For input 16, print Yes. · Đọc tuổi. In Yes nếu tuổi nằm trong khoảng từ 13 đến 19 (thanh thiếu niên), ngược lại là No. Sử dụng and. Với đầu vào 16, in ra Yes.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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. · Đọc một năm bằng int(input()). Một năm chia hết cho 4 khi dư là 0: year % 4 == 0. In ra Leap nếu nó chia hết cho 4, ngược lại in Common. Với dữ liệu đầu vào 2024, hãy in ra Leap.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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. · Chia hết cho 4 không phải là toàn bộ quy tắc. Một năm cũng chia hết cho 100 thì không phải là năm nhuận, trừ khi nó cũng chia hết cho 400. 1900 là bẫy: nó chia hết cho 4 và cho 100, nên in ra Common. In ra Leap hoặc Common.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.