Ask coding questions

← Back to all posts
[python] Can someone explain how if statements with multiple conditions work?
xolyon

Basically I have been working on a piece of code for example

And the output would be something like this 190 , 0

Which is really annoying as it's only following one condition , I tried this with if statements and I got the same thing.

Can explain these multiple Condition concepts to me?

Voters
glitchUnder
xolyon
Comments
hotnewtop
teymour

The OR boolean operator returns true if either of the conditions is correct.
For example if x=2 and y=190 then your function would return true because although y is not less than 100, x is greater than 0.
If you want both x to be greater than 0 and y less than 100 for the condition to be true, try using an AND logical operator (x > 0 and y < 100).

xolyon

@CaptainAnon yes I understand logic operations but if Y is greater than 100 it should end but it doesn't not until x reaches its condition (basically whatever one I put first)

teymour

@xolyon This file cannot be displayed: https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.U4NCIb9_mUdsSXxin02K1AHaEw%26pid%3DApi&f=1
In this case even if y < 100 is false it doesn't matter because x > 0 is true.

xolyon

@CaptainAnon bruh I am legit studyingbthat at school I told you ik what it is but I am confused what you said is correct it should end but it's no an AND statement so if one is True it should end but that's not the case with what I am doing

It just repeats for me until the first condition is met

teymour

@xolyon I'm not exactly sure what you're trying to do. Could you provide some more details?

xolyon

@CaptainAnon ok in a nutshell and OR is basically if one condition is True you it outputs true and in the case of my loop it should end it

However when I try to run my code it only tried to meet the first condition and completely ignored the second one and the OR (so for example the X would reach 0 but the y would already be at like 200) and I don't get what's the problem here and how to fix it

xolyon
teymour

@xolyon A while loop will continue to run as long as the boolean expression you pass to it is true.