I am working on a code where a user needs to enter float as an input in order to calculate pressure for the cycle. I handled things as strings and non-negative inputs but can't figure out how to enable my users to enter floating numbers (2.5, 1.6 etc) without getting an error or getting stuck in a loop. Here's my code:
psrc = input("Enter starting pressure for source: ") # Druck Quelle [Pa] / pressure source
while psrc.isnumeric() == False or float(psrc) <=0 or float(psrc) >=3:
print ("Starting pressure must be numeric value in range 0-3. Please enter valid pressure value.")
psrc = input("Enter new pressure: ")
EDIT: Figured it out. Thank you all for ideas. Problem solved by adding a function which checks for the value and returns boolean to the loop and calculations carry on. Function:
def is_float(n):
try:
float(n)
return True
except:
return False
New loop definition:
psrc = input("Enter starting pressure for source: ") # Druck Quelle [Pa] / pressure source
while is_float(psrc) != True or float(psrc) <=0 or float(psrc) >=3:
print ("Starting pressure must be numeric value in range 0-3. Please enter valid pressure value.")
psrc = input("Enter new pressure: ")