Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Putting a condition
#1
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.")
Reply
#2
while True:
 unit = input("(L)bs or (K)g: ")
 if unit.upper() in ['L','K']:
#Add your code
 else:
  break
Reply
#3
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

Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  putting API results into a dataframe in Python tturner2304 1 930 Aug-01-2025, 11:19 PM
Last Post: Pedroski55
  List / arrays putting in sentence Kurta 3 4,104 Dec-25-2020, 11:29 AM
Last Post: Larz60+
  else condition not called when if condition is false Sandz1286 10 9,524 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 14,709 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 7,647 Apr-10-2020, 02:39 PM
Last Post: buran
  Putting frames on separate lines kenwatts275 3 3,861 Dec-16-2019, 04:52 PM
Last Post: kenwatts275
  reading txt file putting in list function Expel 7 6,390 Jul-17-2019, 03:18 PM
Last Post: Expel
  Putting text on images from excel/csv file sam77am 1 3,419 Apr-05-2019, 07:36 AM
Last Post: Larz60+
  Putting an array for each string that is printed to a loop ClaudioSimonetti 1 3,509 Feb-05-2019, 12:52 PM
Last Post: perfringo
  Putting many coordinates number inside codes fyec 1 3,157 Jun-07-2018, 01:09 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020