Making decisions · Mengambil keputusan
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).
Membuat pilihan
ifmenjalankan kode hanya jika kondisi benar.- Kondisi diikuti oleh titik dua
:. - Baris-baris yang merupakan bagian dari
ifdisisipkan (4 spasi).
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
Perbandingan
- Kondisi biasanya adalah perbandingan yang bernilai Benar atau Salah.
==sama,!=tidak sama,<><=>=.- Catatan:
==menguji kesamaan; satu=menyimpan nilai.
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") memeriksa kondisi lain jika yang pertama salah.elsedijalankan ketika tidak ada kondisi yang benar.- Python memeriksanya secara berurutan dan berhenti pada yang pertama kali benar.
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.
Sisa hasil bagi
a % badalah apa yang tersisa ketikaadibagi denganb.year % 4 == 0berarti tahun habis dibagi 4 — tidak ada sisa.8 % 3sama dengan2, karena 8 adalah dua kali 3 dengan sisa 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.
Menggabungkan kondisi
andbernilai benar hanya jika kedua sisi benar.orbernilai benar jika setidaknya satu sisi benar.notmembalik benar menjadi salah dan salah menjadi benar.
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
Dalam pseudocode Cambridge
- Python
if/elif/elsecocok denganIF … ELSE … ENDIFdanCASEdari ujian.
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.
Kesalahan umum
- Bandingkan dengan
==; satu=melakukan pengisian nilai (assignment). - Tulis
elif, bukanelse if, dan akhiri setiap barisif/elif/elsedengan:. - Blok bersisipan di bawah
ifadalah apa yang dijalankan ketika kondisi benar.
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.
Sekarang Anda coba
- Saat Anda Run, ketik input di kotak di bawah kode dan tekan Enter.
- Pengecekan mengetikkan input untuk Anda. Bacalah, lalu tentukan apa yang harus dicetak.
- Cocokkan teks output tepat.
if / elif / else
Conditions are checked top-down; only the first true branch runs. · Kondisi dicecek dari atas ke bawah; hanya cabang benar pertama yang dijalankan.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass. · Baca skor dengan int(input()). Cetak Pass jika nilainya 50 atau lebih, jika tidak cetak Fail. Untuk input 72, cetak Pass.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Baca skor. Cetak A jika nilainya 80+, B jika 60+, jika tidak C. Gunakan if / elif / else. Untuk input 75, cetak B.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Read an age. Print Yes if the age is from 13 to 19 (a teenager), otherwise No. Use and. For input 16, print Yes. · Baca usia. Cetak Yes jika usianya antara 13 hingga 19 (remaja), jika tidak No. Gunakan and. Untuk input 16, cetak Yes.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Baca tahun dengan int(input()). Tahun habis dibagi 4 jika sisanya 0: year % 4 == 0. Cetak Leap jika habis dibagi 4, jika tidak Common. Untuk input 2024, cetak Leap.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Habis dibagi 4 bukan aturan lengkap. Tahun yang juga habis dibagi 100 adalah bukan tahun kabisat, kecuali juga habis dibagi 400. 1900 adalah jebakan: ia habis dibagi 4 dan 100, jadi cetak Common. Cetak Leap atau Common.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.