0

when i not use .lower(): I don't get the desired result

name=input("Insert your name here: \n")
weight=float(input("Enter your weight here: \n"))
unit=input("What unit do you want to use? \n")

if unit == "kgs" or "KGS":
    print(f"{name} is {weight} kgs.")
elif unit == "LBS" or "lbs":
    convert=weight/2.2
    print(f"{name} is {convert} lbs.")
else:
    print("Please re-enter.")

when I use .lower(): I got the desired result

name=input("Insert your name here: \n")
weight=float(input("Enter your weight here: \n"))
unit=input("What unit do you want to use? \n")

if unit.lower() == "kgs":
    print(f"{name} is {weight} kgs.")
elif unit.lower()== "lbs":
    convert=weight/2.2
    print(f"{name} is {convert} lbs.")
else:
    print("Please re-enter.")

Thanks for the help.

Dejie
  • 11
  • 2
  • Please see https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true which has an explanation. – deltab Jun 13 '20 at 13:55

2 Answers2

1

Change

if unit == "kgs" or "KGS":

to

if unit == "kgs" or unit == "KGS":

You have to write complete expressions on both sides of the or for the entire expression to evaluate the way you expect it to.

Using lower in this case is better though, since it also catches cases where the user types in 'Kgs', for example.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
0

It has least to do with .lower(), you are not using the unit var in the complete expression.

Change this:

if unit == "kgs" or "KGS":

to this (using the unitin the both sides of the or operator):

if unit == "kgs" or unit == "KGS":
DirtyBit
  • 16,613
  • 4
  • 34
  • 55