1

I'm new to Python and trying to create a weight converter by using if, else statements.

This should ask the user to input weight and unit in pounds (lbs) or kilograms (kgs) and convert it, but the program is only executing the if condition.

Here is the snippet of my code:

weight = int(input("Weight = "))
unit = input('(L)bs or (K)')
if unit == "L" or "l":
  print(f'You are {weight*0.45} kilograms')
elif unit == "K" or "k":
  print(f'You are {weight*2.2} pounds')
else:
  print('The converter is only for Kgs and Lbs')

Here is a screenshot:

enter image description here

Malekai
  • 4,765
  • 5
  • 25
  • 60
A_beginner
  • 31
  • 3
  • 2
    Welcome to SO. Please don't post images of code or links to images. Copy and paste as text. [Why not upload images of code on SO when asking a question?](//meta.stackoverflow.com/a/285557) – 001 Jun 10 '19 at 17:25
  • 1
    Also: [How to test multiple variables against a value?](//stackoverflow.com/q/15112125) – 001 Jun 10 '19 at 17:27
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Ari Cooper-Davis Jun 10 '19 at 17:29

4 Answers4

1

You have to write the full condition before and after "or" For the line:

if unit == "L" or "l"

You have to write:

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

also the line:

elif unit == "K" or "k"

to

elif unit == "K" or unit == "k"
0xWise
  • 79
  • 1
  • 6
1

You are lacking parenthesis. Please get familiar with Operator Precedence.

Currently your if-statements are being read by the interpreter as:

if (unit == "L") or "l":
    ...
elif (unit == "K") or "k":
    ...
else:
    ...

and so the first if-statement will always be true since bool("l") == True.

The correct way to do this is:

if unit in "Ll":
    ...
elif unit in "Kk":
    ...
else:
    ...

or even better:

unit = input('(L)bs or (K)g').lower()
if unit == "l":
    ...
elif unit == "k":
    ...
else:
    ...
Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
1

You need to bring change in two lines:

weight  = int(input("Weight: "))
unit    = input("(L)bs or (K): ")

# if unit == "L" or "l":    # OLD_LINE
if unit == "L" or unit == "l":  # NEW_LINE
    print(f"You are {weight*0.45} kilograms")

# elif unit == "K" or "k":  # OLD_LINE
elif unit == "K" or unit == "k":    # NEW_LINE
    print(f"You are {weight*2.2} pounds")

else:
    print("The converter is only for Kgs and Lbs")

See the Reference

Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33
0

When you write:

if unit == 'L' or 'l':

it's getting parsed as:

if (unit == 'L') or ('l'):

which is equivalent to:

if (unit == 'L') or (bool('l') is True):

bool('l') is always true, which is why none of the elif/else blocks get executed.

This is what you intended:

if unit == 'L' or unit == 'l':

If you prefer to avoid the re-use of unit ==, you can re-write it as:

if unit.upper() == 'L':

or even:

if unit in ('L', 'l'):

I hope this helps.

J-L
  • 1,786
  • 10
  • 13