التفريع الشرطي: if و else
يمكن لجملة if أن تحتوي اختياريًا على جزء else. شغّل هذا المثال:
condition = True
if condition:
print('Yes')
else:
print('No')
غيّر الآن السطر الأول إلى condition = False وشغّل البرنامج مرة أخرى.
فكّر في else بمعنى «وإلا» أو «في غير ذلك». فإذا كان الشرط داخل if يساوي False، يُنفذ جسم else بدلًا منه. سواء كان الشرط صحيحًا أو خاطئًا، سيُنفذ واحد فقط من الجسمين.
إليك مثالًا أكثر فائدة؛ شغّله:
sentence = 'Hello World'
excited = True
if excited:
sentence = sentence.upper()
else:
sentence = sentence.lower()
print(sentence)
التعبير sentence.upper() نوع جديد من التعبيرات لم نتعامل معه من قبل. ما يحدث هنا أن sentence نص، والنصوص لها أساليب (Methods) مختلفة تساعدك على حساب قيم جديدة بسهولة، ومنها upper وlower. يشير الاسمان إلى الأحرف الكبيرة (Uppercase) والصغيرة (Lowercase).
مثلًا، التعبير 'Hello World'.upper() تكون قيمته 'HELLO WORLD'. لكنه لا يغيّر محتوى sentence نفسه، ولذلك إذا أردت الاحتفاظ بالقيمة الجديدة فعليك إسنادها من جديد، مثل sentence = sentence.upper().
غيّر الآن excited إلى False وشغّل البرنامج مرة أخرى.
إليك برنامجًا فيه خطأ منطقي:
sentence = 'Hello World'
excited = True
if excited:
char = '!'
sentence += char
print(sentence)
هل ترى المشكلة؟ إذا شغّلته الآن فسيبدو أن كل شيء يعمل. فما الذي يمكن أن يسوء؟
حاول اكتشافه قبل قراءة الإجابة. ماذا يحدث إذا غيّرت excited إلى False؟
إذا كانت excited تساوي True فإن char تُعرّف ويعمل كل شيء بصورة صحيحة. أما إذا كانت False فلن تُسنَد أي قيمة إلى char، ولذلك ستفشل محاولة استخدامها في sentence += char.
أصلح ذلك بإضافة جزء else إلى جملة if، بحيث إذا كانت excited تساوي False تُضاف نقطة (.) إلى نهاية الجملة بدل علامة التعجب (!).
Don't change anything that's already there, just add a bit more code.
elseneeds to come immediately after theifbody, with nothing in between.sentence += charneeds to run whetherexcitedisTrueorFalse.You could have a copy of
sentence += charin both theifandelseblocks, but there's a better way.Use
elseto assign a different value tochar.If
excitedisFalse, thencharshould be'.'instead of'!'.
حان وقت تحدٍّ!
اكتب برنامجًا يستقبل نصًا في المتغير sentence ويطبع نسخة معدلة منه تحتوي على الحروف نفسها، بحيث يكون الحرف الأول كبيرًا (Capitalized) وتكون بقية الحروف صغيرة.
مثلًا، يجب أن تكون النتيجة Hello world سواء كانت قيمة sentence هي 'hello world' أو 'HELLO WORLD'.
You've learned all the tools you need for this. I believe in you! Look at previous programs for inspiration.
You will need a loop to build up the new sentence character by character.
You will need an
if/elseto choose whether to add an uppercase or lowercase character.Your
if/elseneeds to execute different bodies depending on which iteration of the loop it's in.That means that your
ifcondition needs to be a variable that changes inside the loop.In the first iteration you need an uppercase letter. In the following iterations you need a lowercase letter.
ممتاز!
تمرين أخير، وبعده يمكنك الاسترخاء.
اكتب برنامجًا يطبع sentence بأسلوب ساخر، مثل:
OnE MoRe eXeRcIsE, aNd tHeN YoU CaN ReLaX. يجب أن يكون كل محرف ثانٍ بحروف صغيرة، بينما تكون بقية المحارف بحروف كبيرة.
This is similar to the previous exercise. The difference is when and where you set the condition variable.
You will need to have a boolean variable which changes with every iteration.
First write a small program which takes a boolean variable and flips it, i.e. if the variable is
Trueit becomesFalseand if it starts outFalseit's changed toTrue. No loops, just anif/else.You will need to use the variable in the
ifcondition and also assign to the same variable in the body.Combine that flipping
if/elsewith the one that chooses an uppercase or lowercase character.
ممتاز! خذ لحظة لتلاحظ مقدار ما أصبحت قادرًا على كتابته وفهمه بنفسك.