0

nested if statements (python)

What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") why is the answer 3 and not 37?

27th Dec 2018, 2:06 PM
Sridhar Yellayi
Sridhar Yellayi - avatar
3 odpowiedzi
+ 3
num = 7 if num > 3: #True print("3") #prints 3 if num < 5: #False print("5") #skip if num ==7: #skip print("7") #skip The third if condition will only evaluate if it's parent (second if ) is true
27th Dec 2018, 3:11 PM
Dheeraj Tiwari
Dheeraj Tiwari - avatar
+ 2
"If num ==7: print(" 7") " isn't executed because "if num < 5" is false. Reduce the indent of your last if-statement and the 7 will be printed.
27th Dec 2018, 2:26 PM
deha
deha - avatar