0

- I need help in understanding this concept

- when learning.. I was told that if an 'if' function is negated once it will only print out the previous correct 'if'..... here is an example... I don't know why 'result' is being printed x = 4 if x != 3: print("Yes") if x == 5: print("No") if x <= 5: print("result")

12th Sep 2019, 12:04 PM
Blessed-sayah Oyilenaan
Blessed-sayah Oyilenaan - avatar
6 Respuestas
+ 3
Blessed-sayah Oyilenaan here is an example. the last "if" is dependent on the previous one. see how they are nested by indentation. x = 4 if x != 3: print("Yes") if x == 5: print("No") if x <= 5: print("result")
12th Sep 2019, 12:22 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
those are independent "if" statements. each one is evaluated separately the true ones will execute. and since x <= 5 it will also print "result"
12th Sep 2019, 12:12 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
yes, in python blocks of code are determined by indentation. in other programming languages they are mostly defined by { } example: if (some condition) { if(some condition) { } }
12th Sep 2019, 12:33 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Basically, it's going through all of these if-statements because elif isnt used. This also gives it the oppertunity to trigger more than one Consider this: x = 3 if x == 1: print("not excecuted") if x == 3: print("first excecuted") if x >= 2: print("also excecuted") This is because the interpreter evaluates all three statements
12th Sep 2019, 12:19 PM
Trigger
Trigger - avatar
0
- thanks ... can you give me an example of a dependent "if" statement?
12th Sep 2019, 12:14 PM
Blessed-sayah Oyilenaan
Blessed-sayah Oyilenaan - avatar
0
- Thanks.. I've gotten it now... indentation determines if a function is dependent or not
12th Sep 2019, 12:28 PM
Blessed-sayah Oyilenaan
Blessed-sayah Oyilenaan - avatar