Data Validation while loops Data validation Programs get

  • Slides: 4
Download presentation
Data Validation while loops

Data Validation while loops

Data validation • Programs get input data from different sources • All data should

Data validation • Programs get input data from different sources • All data should be ‘validated’ before the program tries to process it • Depends on the specifications – what is valid data in your problem? • May be general “numbers greater than zero” • May be specific “only Y or N allowed”

What to do when invalid? • What does your code do when invalid data

What to do when invalid? • What does your code do when invalid data is seen? • Depends on the specification again • Possible actions for invalid data • • stop the program (with explanatory error message, please!) give the user another chance to enter data (from the keyboard, probably) use a default value for the data and proceed as usual skip the bad data and proceed as usual (not always possible)

Using sentinel logic to validate keyboard input • The requirement is that the user

Using sentinel logic to validate keyboard input • The requirement is that the user must enter Y or N or Q, nothing else • Design ask the user for input while the input is NOT valid display an error message (with acceptable inputs shown) ask the user for input • The trickiest part is the condition for the while • while inp != ‘Y’ and inp != ‘N’ and inp != ‘Q’: is what you need. A common mistake is to use or instead of and • If both cases of letters are allowed, it is simpler to change the input to one case, then test as above (remember the. upper and. lower methods)