Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to check the type of an input, then loop back to the input if the type is incorrect?

def set_values():
    loopCtrl = 1
    while loopCtrl == 1:
        loopCtrl = 2
        juvenilePopulation = int(input("Enter the population of the juvenile greenfly (1000s) > "))
        if not isinstance(juvenilePopulation, int): loopCtrl = 1
        juvenileSurvivalRate = float(input("Enter the survival rate of juvenile greenfly (decimal) > "))
        if not isinstance(juvenileSurvivalRate, float): loopCtrl = 1
        adultPopulation = int(input("Enter the population of the adult greenfly (1000s) > "))
        if not isinstance(adultPopulation, int): loopCtrl = 1
        adultSurvivalRate = float(input("Enter the survival rate of adult greenfly (decimal) > "))
        if not isinstance(adultSurvivalRate, float): loopCtrl = 1
        senilePopulation = int(input("Enter the population of the senile greenfly (1000s) > "))
        if not isinstance(senilePopulation, int): loopCtrl = 1
        senileSurvivalRate = float(input("Enter the survival rate of senile greenfly (decimal) > "))
        if not isinstance(senileSurvivalRate, float): loopCtrl = 1
        birthRate = float(input("Enter the birthrate of the greenfly > "))
        if not isinstance(birthRate, float): loopCtrl = 1

I have this admittedly ugly chunk of code, that currently just asks for a bunch of inputs, assigns it to variables, and then checks the type of the variable, before looping back around to the top. What I really want to achieve is for the code to loop back to the input statement that had the incorrect input rather than the beginning, but in a way that is more pythonistic than a ton of while loops.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Replace your calls to input with a function like this:

def get_value(prompt, validator):
    while True:
        try:
            return validator(input(prompt))
        except ValueError as err:
            print(f"  Invalid value, please try again: {err}")

You would call it like this:

juvenilePopulation = get_value("Enter the population of the juvenile greenfly (1000s) > ", int)
juvenileSurvivalRate = get_value("Enter the survival rate of juvenile greenfly (decimal) > ", float)

Running the above code looks something like this:

Enter the population of the juvenile greenfly (1000s) > foo
  Invalid value, please try again: invalid literal for int() with base 10: 'foo'
Enter the population of the juvenile greenfly (1000s) > 1.1
  Invalid value, please try again: invalid literal for int() with base 10: '1.1'
Enter the population of the juvenile greenfly (1000s) > 12
Enter the survival rate of juvenile greenfly (decimal) > bar
  Invalid value, please try again: could not convert string to float: 'bar'
Enter the survival rate of juvenile greenfly (decimal) > 0.1

Note that in this example we’re using basic types like int and float for validation, but you could just as easily pass in a custom function. For example, if the survival rate needs to be between 0 and 1, you could write:

def validateSurvivalRate(v):
    v = float(v)
    if not 0 < v < 1:
      raise ValueError("surival rate must be between 0 and 1")
    return v

juvenileSurvivalRate = get_value("Enter the survival rate of juvenile greenfly (decimal) > ", validateSurvivalRate)

Which would look like:

Enter the survival rate of juvenile greenfly (decimal) > foo
  Invalid value, please try again: could not convert string to float: 'foo'
Enter the survival rate of juvenile greenfly (decimal) > 1.1
  Invalid value, please try again: surival rate must be between 0 and 1
Enter the survival rate of juvenile greenfly (decimal) > -4
  Invalid value, please try again: surival rate must be between 0 and 1
Enter the survival rate of juvenile greenfly (decimal) > 0.4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading