1

A weight is given. It could be either in lbs or kg. The code then converts accordingly (ie lbs to kg; kg to lbs). In the code, lbs has the unit inputs: l or L. kg has the unit inputs: k or K.

weight = 60
unit = "k"

if unit == "L" or "l":
    weight_kg = weight * 0.45
    print(f"You are {weight_kg}kg")
else:
    weight_lb = weight * 2.2
    print(f"You are {weight_lb}lbs")

But this returns: you are 27.0kg. The code still executes the if statement though the unit provided is not "L" or "l".

I then tweaked the if statement to this:

if unit == "L" or unit == "l":

and it now gives the correct 132.0lbs.

I have tried looking through online tutorials and my notes, but still don't understand why the first code didn't work and why the second one worked...

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
toothpick
  • 125
  • 1
  • 7
  • 1
    This question and its answer also gave me a clearer picture: [Why does `a == x or y or z` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – toothpick May 18 '21 at 20:37

1 Answers1

0

The basic idea is that "l" is a truthy Value

> if unit == "L" or "l":

Before you tweaked your code it returns: you are 27.0kg. and the code still executed the if statement though the unit provided is not "L" or "l" because you didn't specify whether any variable is equal to (comparison operator) "l" or not. So on execution, it always executed True being a truthy value. As it is OR operator if unit=="L" one operand gets true and if unit!="L" still the other operand is always true, so the if statement runs.

If you did the same thing with AND operator , it would have executed the else statement.

Here's an example:

Input:

> if 'l' or 'L':
>     print("HI") 
> else:
>     print("HELLO")

Output: HI

Here the output is always "HI" as both sides of operator always execute True.

Trying with AND operator:

Input:

> weight = 60 unit = "k"
> 
> if unit == "L" and "l":
>     weight_kg = weight * 0.45
>     print(f"You are {weight_kg}kg") 
> else:
>     weight_lb = weight * 2.2
>     print(f"You are {weight_lb}lbs")

Output: You are 132.0lbs

I didn't mean to spoil the logic here. I just mean to show you that with AND operator both sides True will be needed and only "l" executes True so it executes else statement.

Truthy Values: According to the Python Documentation: By default, an object is considered true. Truthy values include: 1.Non-empty sequences or collections (lists, tuples, strings, dictionaries, sets).

2.Numeric values that are not zero.

3.True

Amisha Kirti
  • 132
  • 1
  • 9
  • Thank you for your reply, but what do you mean by _**"l" actually equals something or not**_? Does that mean I should assign something to "l"? Should I put this: `unit == "l"` as I did for "L" in the if statement? Is that why after tweaking the code, I got the correct 132lbs? – toothpick May 16 '21 at 16:42
  • Sorry, my bad. I didn't write it correctly, I meant to say whether any variable is equal to "l" or not. And yes ,by writing unit == "l" , you got correct answer because "l" is a truthy value. – Amisha Kirti May 16 '21 at 17:10
  • I see that you are a new user. So , I would like to give a suggestion that if any answer or question fulfills what you wanted to know you can click upvote. It encourages! – Amisha Kirti May 16 '21 at 19:32
  • Ohh, does equal in _**variable is equal to "l"**_ refer to assignment operator (=) or comparison operator (==)? So sorry for the questions m(。≧Д≦。)m – toothpick May 16 '21 at 19:45
  • icic... I don't think I have enough points to vote yet – toothpick May 16 '21 at 19:46
  • No issues. It's comparison operator. I updated in the answer. – Amisha Kirti May 16 '21 at 19:47