Python Forum

Full Version: Putting a condition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Python Tutorial for Beginners [Full Course] Learn Python for Web Development
can you help me with the following code: above is youtube video.
you know how you have a weight and unit excersice under if statement in this video above. i was trying that user should only enter "K" or "L for unit of weight and untill they dont enter "K" or " L" terminal should keep asking for the right unit. Please help.
(Python)
weight = int(input("weight: "))
unit = input("(L)bs or (K)g: ")
if unit.upper() == "l":
converted = weight * 0.45
print(f"you are {converted} Kilos")
else:
converted = weight / 0.45
print(f"You are {converted} Pounds.")
while True:
 unit = input("(L)bs or (K)g: ")
 if unit.upper() in ['L','K']:
#Add your code
 else:
  break
I tried but doesn't work.
pls, help.

while True:
weight = int(input("weight: "))
unit = input("(L)bs or (K)g: ")
if unit.upper() == in ["L"]:
converted = weight * 0.45
print(f"you are {converted} Kilos")
elif unit.upper() == in ["K"]:
converted = weight / 0.45
print(f"You are {converted} Pounds.")
else:
break
(Apr-24-2020, 11:56 PM)Jiwan Wrote: [ -> ]I tried but doesn't work.
pls, help.

while True:
weight = int(input("weight: "))
unit = input("(L)bs or (K)g: ")
if unit.upper() == in ["L"]:
converted = weight * 0.45
print(f"you are {converted} Kilos")
elif unit.upper() == in ["K"]:
converted = weight / 0.45
print(f"You are {converted} Pounds.")
else:
break
Removed == from my previous post. Use either "==" or "in" operator. Use "==" when you want to compare single value and use "in" operator when you want to compare against multiple values