بعض القرارات تأتي على مراحل: أولًا نتأكد أن الفرن ساخن بما يكفي، وبعد ذلك فقط نسأل هل وقت الطهي كافٍ. لذلك نكتب جملة if داخل جملة if.
temperature = 180
cook_time = 12
if temperature >= 170:
print("Heating temperature OK")
if cook_time >= 12:
print("Cooking successful")Heating temperature OK Cooking successful
الشرط الداخلي لا يُفحص أصلًا إلا إذا تحقق الشرط الخارجي. والأوامر التابعة للشرط الداخلي تُزاح مرتين (8 مسافات).
else داخل if
داخل if الخارجية يمكن كتابة if و elif و else كاملة. الإزاحة هي التي تحدد لمن تتبع كل else:
weather = "sunny"
team = "Green Team"
if weather == "sunny":
if team == "Red Team":
print("Destination: the farm")
elif team == "Blue Team":
print("Destination: the science museum")
else:
print("Destination: the aquarium")
else:
print("Field trip canceled")Destination: the aquarium
| الجزء | الإزاحة | يتبع |
|---|---|---|
| else الداخلية | 4 مسافات | if team == ... |
| else الخارجية | بدون إزاحة | if weather == ... |
العدّ داخل الشرط
العدّاد متغير يبدأ من 0، ونزيده بمقدار 1 كلما تحقق شرط: count = count + 1. ولطباعته مع نص نحوّله بـ str():
bug = 45
rare_count = 0
if bug >= 45:
print("Got a Rare Beetle!")
rare_count = rare_count + 1
print("Rare Beetle Counter:" + str(rare_count))Got a Rare Beetle! Rare Beetle Counter:1
يمكن أن نفحص أكثر من قيمة بجمل if منفصلة، فيعدّ العدّاد كل قيمة تحقق الشرط:
passed = 0
math = 72
science = 45
english = 90
if math >= 50:
passed = passed + 1
if science >= 50:
passed = passed + 1
if english >= 50:
passed = passed + 1
print("Subjects passed: " + str(passed))Subjects passed: 2
تدريبات
داخل الشرط أضف: إذا كانت cook_time تساوي 12 أو أكثر اطبع Cooking successful.
temperature = 180
cook_time = 12
if temperature >= 170:
print("Heating temperature OK")اضغط «تشغيل» لترى النتيجة. أسند 90 إلى score و 1 إلى rank. إذا كانت score تساوي 85 أو أكثر اطبع Certification passed، وداخل هذا الشرط: إذا كانت rank تساوي 1 اطبع Trophy earned.
اضغط «تشغيل» لترى النتيجة. إذا لم يكن المتجر مفتوحًا (والجو مشمس) اطبع Buy snacks at the convenience store and go stargazing.
weather = "sunny"
shop = "closed"
if weather == "sunny":
if shop == "open":
print("Buy a star chart and go stargazing")
___
___
else:
print("Planetarium DVD at home")اضغط «تشغيل» لترى النتيجة. داخل شرط الجو المشمس: إذا كان الفريق "Red Team" اطبع Destination: the farm، وإذا كان "Blue Team" اطبع Destination: the science museum، وإلا اطبع Destination: the candy factory.
weather = "sunny"
team = "Green Team"
if weather == "sunny":
if team == "Red Team":
print("Destination: the farm")
else:
print("Field trip canceled")اضغط «تشغيل» لترى النتيجة. إذا كانت bug تساوي 90 أو أكثر: اطبع Super Rare Beetle Get!، وزد super_rare_count بمقدار 1، ثم اطبع "Super Rare Beetle Counter:" + str(super_rare_count).
bug = 95 super_rare_count = 0
اضغط «تشغيل» لترى النتيجة. نتيجة طالب: total = 380 و absent_days = 4. إذا كان المجموع 250 أو أكثر: إذا كانت أيام الغياب 10 أو أقل اطبع Passed، وإلا اطبع Passed, but too many absences. وإذا كان المجموع أقل من 250 اطبع Second round exam.
total = 380 absent_days = 4
اضغط «تشغيل» لترى النتيجة. ثلاث مباريات للفريق: match1 = "win" و match2 = "loss" و match3 = "win". ابدأ wins = 0، وزده بمقدار 1 لكل مباراة قيمتها "win" (ثلاث جمل if)، ثم اطبع Wins: 2.
match1 = "win" match2 = "loss" match3 = "win"
اضغط «تشغيل» لترى النتيجة. النقاط الرئيسية
- if داخل if: الشرط الداخلي يُفحص فقط إذا تحقق الخارجي.
- الإزاحة تحدد لأي if تتبع كل else أو elif.
- العدّاد يبدأ من 0 ويزيد بـ
count = count + 1داخل الشرط، ونطبعه مع نص بـstr().