Night 5: Is your pet hungry?
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
Tonight: help your pet decide
- Is your pet hungry or happy? It depends on its energy.
- You will learn one thing:
ifandelse.
Words for tonight
| Word | Meaning |
|---|---|
| if | only when something is true |
| else | in all other cases |
| indent | 4 spaces at the start of a line |
English
if asks a question
ifasks a question. The answer isTrueorFalse.- When it is
True, Python runs the indented lines below. - The line with
ifends with a colon:.
energy = 2
if energy < 5:
print("I am hungry")
print("Bye!")
print("Bye!")has no indent. It always runs.
English
else is the other way
else:runs when the question isFalse.- Python runs one of the two blocks, never both.
energy = 8
if energy < 5:
print("I am hungry")
else:
print("I am happy")
English
Question signs
| Sign | Question | True example |
|---|---|---|
< |
less than? | 3 < 5 |
> |
more than? | 9 > 5 |
== |
the same? | "moon" == "moon" |
!= |
different? | "sun" != "moon" |
<= |
less than or the same? | 5 <= 5 |
English
= or ==?
- One
=puts a value in a box:word = "moon". - Two
==ask a question:if word == "moon": - Text must match exactly.
"Moon"and"moon"are different.
English
Now you try
- Do the tasks below. Press Run, then Check answer.
- Watch the indent: 4 spaces after every line that ends with
:.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read the energy with int(input()). If energy is less than 5, print I am hungry. Otherwise, print I am happy.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Your pet guards a door. Read a word with input(). If the word is moon, print Come in!. Otherwise, print Go away!.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read the energy with int(input()). Below 3: print Very hungry. Below 7: print OK. Otherwise: print Full of energy. Use if, elif และ else.
คลิก Run เพื่อดูผลลัพธ์ที่นี่