Often, you’ll need to test more than two possible situations, and to
evaluate these you can use Python’s if-elif-else syntax. Python executes
only one block in an if-elif-else chain. It runs each conditional test in
order until one passes. When a test passes, the code following that test
is executed and Python skips the rest of the tests.
Many real-world situations involve more than two possible conditions. For example, consider an amusement park that charges.
fferent rates for different age groups:
Admission for anyone under age 4 is free.
Admission for anyone between the ages of 4 and 18 is $25.
Admission for anyone age 18 or older is $40.
غالبًا ما ستحتاج إلى اختبار أكثر من حالتين محتملتين، ولتقييم هذه الحالات يمكنك استخدام صيغة if-elif-else في بايثون. ينفذ بايثون كتلة واحدة فقط في سلسلة if-elif-else. يقوم بايثون بتشغيل كل اختبار شرطي بالتتابع حتى ينجح أحدها. عندما ينجح اختبار ما، يتم تنفيذ الكود التالي لهذا الاختبار ويتخطى بايثون باقي الاختبارات.
تتضمن العديد من الحالات الواقعية أكثر من شرطين محتملين. على سبيل المثال، فكر في مدينة ملاهٍ تفرض أسعارًا مختلفة لمجموعات عمرية مختلفة:
الدخول مجاني لأي شخص تحت سن 4 سنوات.
الدخول لأي شخص بين سن 4 و18 يكلف 25 دولارًا.
الدخول لأي شخص عمره 18 عامًا أو أكثر يكلف 40 دولارًا.
#python
#programming
#pythonprogramming