I'm having some trouble with AND and OR within if else statements. This is my code:
if subject == 'history' or subject == 'History' and unit == 'WWII' or unit == 'ww2':
Any ideas how to fix this?
Read up on operator-precedence - some operators bind stronnger then others - to get your desired logical output you should use ( ) to structure your logical conditions.
Summarization:
Operator Description lambda Lambda expression if – else Conditional expression or Boolean OR and Boolean AND not x Boolean NOT in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity tests | Bitwise OR ^ Bitwise XOR & Bitwise AND <<, >> Shifts +, - Addition and subtraction *, @, /, //, % Multiplication, matrix multiplication, division, floor division, remainder [5] +x, -x, ~x Positive, negative, bitwise NOT ** Exponentiation [6] await x Await expression x[index], x[index:index], x(arguments...), x.attribute Subscription, slicing, call, attribute reference (expressions...), [expressions...], {key: value...}, {expressions...} Binding or tuple display, list display, dictionary display, set display
The ealier the operator the higher it binds. Please see the linked doku for subscription context
userName, subject, unit, score, grade = hisWw2[:5]and,or). The may not always be necessary but in complex expressions they are helpful to understand the expressionelifandbinds stronger thenordoes .... use () -- weight is the wrong word - brb - see answer for doku on pythons operators.