תנאים מקוננים (nested ifs) - תנאי בתוך תנאי

תנאים מקוננים (nested ifs) - תנאי בתוך תנאי#

צפו בסרטון הבא המסביר איך ניתן לממש תנאים שתלויים אחד בשני:

לנוחותכם חלונית עם הקוד שראינו בסרטון. מוזמנים להריץ בעצמכם:

temp = 25
wait = 60

if temp < 20:
    print("Eat soup!")
    print("Enjoy!")
    if wait >= 40:
        print("Order a delivery")
    else:
        print("Prepare dinner yourself")
else:
    print("Eat a salad!")
print("Bon appetit!")
Eat a salad!
Bon appetit!

נסו בעצמכם

הקוד הבא בודק עבור מספר השמור בn האם הוא מתחלק ב2, 3 ו6 ומדפיס את הטקסט המתאים

n=12

if n % 3 == 0: 
    if n % 2 == 0: # NESTED if
        print('divisible by 2,3,6')
    else: # NESTED else, pertains to INNER if.
        print('divisible by 3')
else: # Not nested, thus pertains to OUTER if.
    if n % 2 == 0: # NESTED if
        print('divisible by 2')
    else:
        print(n + ' is not divisible by 3.')
divisible by 2,3,6