0

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: ")
martineau
  • 119,623
  • 25
  • 170
  • 301
Fatima
  • 61
  • 7
  • share error you are getting for the given input – sahasrara62 Aug 09 '21 at 07:56
  • If I type integers, it passes, but any floating number just keeps looping. For example: 2 would complete the calculation but, 2.1 would loop through since isnumeric() returns false. Is there any way to bypass this? Without isnumeric(), I'm getting ValueError, can't cast string to float. – Fatima Aug 09 '21 at 08:01

5 Answers5

1

str(int(psrc)).isnumeric() will help you solve issue i guess.

  • ```int``` will fail if there are letters or anything else in the string –  Aug 09 '21 at 08:13
1

Check this out

x=False
while not x:
    try:
        psrc = float(input("Enter starting pressure for source: ")) # Druck Quelle [Pa] / pressure source 
        x=True
    except ValueError:
        print('Enter valid decimal number.')
print(psrc)
  • @fatima. Yep, took advantage of the fact that ```float``` raises a value error if it can't convert it to decimals –  Aug 09 '21 at 08:16
1

i can offer you two ways: 1.

def is_it_float(num:str):
    if num.count(".")>1:
        return False
    num = num.split(".")
    for i in num:
        if not i.isnumeric():
            return False
    return True
def is_it_float(num:str):
    try:
        float(num)
        return True
    except ValueError:
        return False
yotam rec
  • 587
  • 4
  • 15
1

psrc.isnumeric() checks if the string only contains numbers so it returns False because of the decimal separator. You need to define a function that checks if your string is a float and use that instead:

while is_string_float(psrc) == False or float(psrc) <=0 or float(psrc) >=3: 
...

Is string a float: Checking if a string can be converted to float in Python

Tzane
  • 2,752
  • 1
  • 10
  • 21
0

You can try to store the answer internally, then check for decimals, and if they occur, convert to string.

  • That's a nice idea but I'm not sure how to do it properly considering this loop is in a bigger loop and several other inputs depend on this one. I need a fast calculation but still handle various inputs. – Fatima Aug 09 '21 at 08:07