"OR" operator won't seem to work, why?

Hi guys,
I am quite new to Python and I was doing an exercise. The task is to create a convertor that converts your weight from Kg to Lbs or the other way around. The input from the user should not be case sensitive.

You can see the task description in the video here: Python Tutorial - Python for Beginners [Full Course] - YouTube

My solution is quite similar to what the guy wrote but for some reason the “or” operator doesn’t seem to work. Can someone explain why?
Here is my code:

Weight = float(input('Weight: '))
choice = input('(L)bs or (K)g: ')
KG = (Weight * 0.453592)
Lbs = (Weight * 2.20462)
if choice == 'L' or 'l':
    print(f"You are {KG} kilos")
else:
    print(f"You are {Lbs} pounds")

Haven’t you got the if and else the wrong way round?

No, it converts the one to the other, look at the definitions for KG and Lbs.
If you run it without the “or” like this:
if choice.upper == ‘L’

it all works great.

This line is parsed the same as:
if (choice == 'L') or ('l'):

If either parenthesized term is true, the if statement will follow the conditional. And the string 'l' is always true.

You probably are intending
if choice == 'L' or choice == 'l':
or you can create a collection and say:
if choice in ('L', 'l'):

2 Likes

Thank you! That was very helpful!