简体   繁体   中英

How do I get a validated input to move onto the next input rather than starting a new range?

Bascially, I'm building a car sales program where cars and their details are added to a class (and then a text file, but that part isn't relevant for what I need help with). I'm using a for loop 'for x in range(nocars)'to do this. My inputs all work without the validation but now I need to put validation in which is what I've done with the 'carYear' variable as you can see with a while loop. Basically if the year is below 1900 it will keep prompting the user to re-input a correct value (each time they enter incorrectly). This does work in the program so far. Once they do enter a correct number, the program should move onto the next input down for the next variable. However instead it just starts the range process again. I know this is caused by the 'break' but I'm not sure of any other way to exit the while loop while still resuming the input process. Help? While loop is surrounded by double asterisks so you can find it. Thanks.

 for x in range(nocars):
        print("Car No.", x+1)
        carMake = input("Please enter make: ")
        carModel = input("Please enter model: ")
        carYear = input("Please enter year of manufacture: ")
        **while int(carYear) < 1900 :
            carYear = input("Please enter a valid value: ")
        else:
            break**
        carLitre = input("Please enter the cylinder capacity of the car in litres : ")
        carVal = input("Please enter value in GBP: ")
        carReg = input("Please enter registration number (excluding spaces): ")
        carOwn = input("Please enter the previous ownership (new, second-hand, third-hand, more than three previous owners): ")
        carMile = input("Please enter the mileage: ")
        carMod = input("Please enter any modiciations existing on the car. If there are no modifications, enter 'None': ")                    
        carCond = input("Please enter the condition of the car (VP = Very Poor, P = Poor, A = Average, G = Good, VG = Very Good, P = Pristine: ")
        print("-Customer Details-")
        cusName1 = input("Please enter your forename: ")
        cusName2 = input("Please enter your surname: ")
        cusGen = input("Please enter your gender (Male, Female, Other): ")
        cusAge = input("Please enter your age: ")
        print ("----------")
        print("You have added car(s) to temporary memory. To save this to a text file, select '3' from the following menu and follow the steps...")

        carList.append(CarEntry(carMake, carModel, carYear, carLitre, carVal, carReg, carOwn, carMile, carMod, carCond, cusName1, cusName2, cusGen, cusAge))        

You should do your validating in a different function, like this:

carYear = validNumberInput('Please enter the year of manufacture: ', 1900)

With the function separated like so:

def validNumberInput(prompt, minimum):
    while True:
        try:
            value = int(input(prompt))
        except ValueError:
            continue
        if value >= minimum:
            return value

Then call the function with different prompts as needed.

You would need to write different functions for different types of validations, such as empty strings, invalid numbers, number ranges, etc.

the while-else construct probably isn't doing what you want. the break following the else actually causes the outer for loop to break. Try getting rid of the else part so it looks like:

while int(carYear) < 1900 :
     carYear = input("Please enter a valid value: ") 
carLitre = input("Please enter the cylinder capacity of the car in litres : ")
carVal = input("Please enter value in GBP: ")
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM