0

My code doesn't work as it always goes through the if section even when "K" or "k" is typed.

Weight = int(input("What's your weight?")) 
Unit = input('Type "L" for lbs, "K" for kilograms: ')

if Unit == "L" or "l":
     print(f' You are {Weight / 2.205} Kilograms!')

elif Unit == "K" or "k":
    print(f' You are {Weight * 2.205} pounds!')

else:
    print('You can only type "L", "l", "K" or "k".')

When you type your weight in kilograms it goes thru if section. I just started your help is much appreciated.

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
Deniz
  • 17
  • 5

2 Answers2

1

Modify your if statements:

if Unit == "L" or Unit == "l":
    # code

Therefore the others:

if Unit == "L" or Unit == "l":
     print(f' You are {Weight / 2.205} Kilograms!')

elif Unit == "K" or Unit == "k":
    print(f' You are {Weight * 2.205} pounds!')

Addition to the question:

Use .lower() instead of checking if Unit equals both upper and lower case separately

if Unit.lower() == "l":
    # code

elif Unit.lower() == "K":
    # code
Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28
  • 1
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section _Answer Well-Asked Questions_, and the note therein regarding questions that have "been asked and answered many times before". – Charles Duffy Aug 01 '22 at 03:41
  • Thank you sense. How could I miss that? I changed if but not elif and everything worked. – Deniz Aug 01 '22 at 03:43
-4

Try adding another '=' equal sign for being strict, cause you are basically saying 'if Unit is a String', adding the third = will say 'if Unit is strictly equal to L'

Weight = int(input("What's your weight?")) 
Unit = input('Type "L" for lbs, "K" for kilograms: ')

if Unit === "L" or "l":
     print(f' You are {Weight / 2.205} Kilograms!')

elif Unit === "K" or "k":
    print(f' You are {Weight * 2.205} pounds!')

else:
    print('You can only type "L", "l", "K" or "k".')
KetZoomer
  • 2,701
  • 3
  • 15
  • 43