Someone check this:

if fuel_type == (“A” or “a”):
print(“You have chosen Unleaded Petrol”)

this won’t work. First the expression between parentheses is evaluated, which will result in "A", then "A" and only A (the uppercase) is checked if it equals fuel_type

There are several options, you could compare with fuel_type at both sides of the or operator

you could use in to check if in a list, so then you make a list or tuple with the valid options

you could use .lower() to convert fuel_type to lower, even in the comparison, then only for the comparison fuel_type will temporary be lowered, this won’t persist. (using .upper() is of course also possible)

1 Like

If we examine the bracketed expression first, as precedence dictates, we have,

"A" or "a"

This is a boolean expression. Trouble is, though, “A” is not an empty string so gets cast to True and short-circuits the expression to True. Now put this back into the statement…

if fuel_type == True:

The evaluation will involve casting the left side expression to a boolean so if it is non-zero or not an empty string it too will cast to True. One can see there is not a lot of decision making here. Assuming the variable is always a value other than 0 or "" the result will always be True.

This conflicts slightly with what @stetim94 wrote above, but only because the bracketed expression yields a boolean, not an, "A". By following the advice to compare the variable to each operand in a separate expressions we get a result that can feasibly be one of True or False so our logic swings freely from one outcome path to the other.

if x == "A" or x == "a":


There are alternate approaches to logic, one of which uses the Python in operator.

if fuel_type in "Aa":

will be True for either “A” or “a”. Something to keep in mind as you go forward.

1 Like