Is this correct? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is this correct?

x=7 if x>5: print("bigger than 5") if x>3: print("bigger than 3") >>> bigger than 5 >>> biggrt than 3

23rd Sep 2018, 6:27 AM
Hamed Mamipour
2 Answers
+ 1
Change position of 3 and 5 otherwise 4 will not be seen. First if checks > 5 so only numbers higher are treated in the loop. 4 < 5 so the loop ends there. Why not try it out in the code playground and see for yourself?
23rd Sep 2018, 6:32 AM
davy hermans
davy hermans - avatar
+ 1
It is correct but kind of pointless because if x is bigger than 5, it will always be bigger than 3. Note that because of the indentation lines 4 and 5 will only be executed if x is bigger than 5. If you want to check if x is bigger than 3 even if it is not bigger than 5, you need to change the indentation: x=7 if x>5: print("bigger than 5") if x>3: print("bigger than 3") If you want to check if x is bigger than 3 ONLY if it is not bigger than 5, use "elif x>3:" instead.
23rd Sep 2018, 6:38 AM
Anna
Anna - avatar