What's poppin my coding gang. So atm I'm learning Python and I'm a totally a newbie and I face this problem. So I created a unit converting program and I was successful to make a while loop for unit and everything with the code below works just fine:
weight = int(input("Weight: "))
unit = input("(K)g or (L)bs ? ")
while unit.upper != ("K" or "L"):
if unit.upper()=="L":
converted = weight*0.45
print(f"You are {round(converted)} kilos")
break
elif unit.upper()=="K":
converted=weight//0.45
print(f"You are {converted} pounds")
break
else:
print("Invalid unit. Please type K or L: ")
unit=input()
continue
But I also wanted to experiment more and I also wanted to create a while loop for a weight input, so that it will go forever until you type any positive float or integer number, because when I run the program and in weight input I would accidently type a letter - a big red error would appear on my screen saying:
Exception has occurred: ValueError
invalid literal for int() with base 10: 'a'
line 1, in <module>
weight = int(input("Weight: "))
So when I tried to change it to a while loop, it didn't work and my final result looked like this:
weight = int(input("Weight: "))
while weight != int():
if weight==int():
break
else:
print("Invalid unit. Please type a number: ")
weight=int(input())
continue
unit = input("(K)g or (L)bs ? ")
while unit.upper != ("K" or "L"):
if unit.upper()=="L":
converted = weight*0.45
print(f"You are {round(converted)} kilos")
break
elif unit.upper()=="K":
converted=weight//0.45
print(f"You are {converted} pounds")
break
else:
print("Invalid unit. Please type K or L: ")
unit=input()
continue
I know it's shit and at this point I'm stuck, it's just constantly typing me "Invalid unit. Please type a number: " and I can't get out of that loop. I don't even know what to type or what to do anymore so I decided to come here for a help.
I want to make with this code that until you type a number in weight input - you won't be allowed to go further, but after you type correctly - the program will continue to a unit input. Thx