Practicing with an exercise and ran into something I can't really figure out(Beginner)

Within the code above whenever I type in “L” or “l” into the terminal I’m given the results for the Kgs when it should produce the results for the Lbs

Though when I instead use( As hopefully shown in

weight = float(input("Weight: “))
unit = input(”(K)g or (L)bs: ")

if unit.upper() == == “K”:
weight *= .45
print("Weight in kgs: " + str(weight))
elif unit.upper() == “L”:
weight /= .45
print("Weight in lbs: " + str(weight))

The code works perfectly fine and was wondering why this is?

To preserve code formatting in forum posts, see: [How to] Format code in posts

Your condition

if unit == "K" or "k":

is equivalent to

if (unit == "K") or ("k"):

The second operand ("k") is truthy. Therefore the above condition will always evaluate to True irrespective of the value assigned to unit.

(See the paragraph titled “Truth Value Testing” in the documentation to see which values are considered Falsy. The rest are Truthy).

If you wish, you can write the condition as:

if (unit == "K") or (unit == "k"):

Understood and thank you for taking the time to give me the answer!

1 Like