While Loop program

In the weight conversion program, can you give me code that returns “Not a valid input” each time i put a string value in weight and returns to “enter your weight” .also “Not a valid input” each time i enter a integer or float in unit and returns to “enter unit” again.

Could you post your code

This seems to work.

weight = 0
unit = “”

while True:
while True:
weight = input('Enter your weight: ')
try:
int(weight)
except ValueError:
print(“Not a valid input. (hint: this should be a number only)”)
else:
break
while True:
unit = input("What is the unit of weight L(pounds) or K(kilograms)? ").upper()
if unit != (“L” or “K”):
print(“Enter L for pounds or K for kilograms.”)
else:
break
break

Thank you for the reply. Since the indents are not clear. I’m unable to debug this as I’m a beginner. Can you give some kind of clear screenshot.

No, please don’t post screenshots of code. You can’t search screenshots. You can’t copy&paste the code in screenshots but have to type it out to try the code. Use code fences to properly format code here.

1 Like

Sorry,

This was my first post. I’m new to Python and don’t code professionally or anything, so I’m sure the code below could be better. Thanks for the link SAM.

weight = 0
unit = ""

while True:
	while True:
		weight = input('Enter your weight: ')
		try:
		  int(weight)
		except ValueError:
		  print("Not a valid input. (hint: this should be a number only)")
		else: 
		  break
	while True:
		unit = input("What is the unit of weight L(pounds) or K(kilograms)? ").upper()
		if unit != ("L" or "K"):
			print("Enter L for pounds or K for kilograms.")
		else:
			break	
	break
while True

is not very pythonic. Check this post where I show a differente way to handle the while loop.