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")
6 Answers
+ 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")
+ 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"
+ 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) {
}
}
+ 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
0
- thanks ... can you give me an example of a dependent "if" statement?
0
- Thanks.. I've gotten it now... indentation determines if a function is dependent or not