0

So I was trying to make a weight converter application. It first asks the user their weight, and then asks if it is in Lbs or Kg and then it converts it to the other unit. For example, if someone entered their weight in Kg, it will convert it in Lbs. But the problem is, whenever I enter in pounds, its supposed to convert it to kg but it does not, and "converts" it to pounds, even though it is in pounds.

weight = int(input("Enter your weight: "))
user_choice = input ("(L)bs or (K)g: ")

if user_choice == "K" or "k":
    converted = int(weight) * 2.2046
    print (f"Your weight in pounds is {converted} lbs")
elif user_choice == "L" or "l":
    converted = int(weight) / 2.2046
    print (f"Your weight in kilograms is {converted} kg")
else:
    print ("Please enter a valid option.")

I'm a beginner in python so any help will be appreciated.

  • 1
    The if conditions should be `user_choice == 'K' or user_choice == 'k'` or `user_choice in ['K', 'k']`. – Zoro May 15 '21 at 06:17
  • This is becaue `or "k"` is being evaluated to True because `"k"` isn't False. Try `False or True` to see what's happening – Ben May 15 '21 at 06:18

2 Answers2

1

Use user_choice=="k" or user_choice=="K":

    weight = int(input("Enter your weight: "))
    user_choice = input ("(L)bs or (K)g: ")
    
    if (user_choice == "K" or user_choice=="k"):
        converted = int(weight) * 2.2046
        print (f"Your weight in pounds is {converted} lbs")
    elif(user_choice == "L" or user_choice=="l"):
        converted = (int(weight) / 2.2046)
        print (f"Your weight in kilograms is {converted} kg")
    else:
        print ("Please enter a valid option.")

anirudh
  • 1,436
  • 5
  • 18
0

You could make it work by modifying the if-elif conditions a bit

weight = int(input("Enter your weight: "))
user_choice = input ("(L)bs or (K)g: ")

if user_choice in ('k', 'K'):
    converted = int(weight) * 2.2046
    print (f"Your weight in pounds is {converted} lbs")
elif user_choice in ('l', 'L'):
    converted = int(weight) / 2.2046
    print (f"Your weight in kilograms is {converted} kg")
else:
    print ("Please enter a valid option.")
Anamitra Musib
  • 336
  • 4
  • 5