CONCEPT: input validation is the procedure of inspecting data that has been input come a program, to make sure it is valid prior to it is provided in a computation. Input validation is generally done through a loop that iterates as long as one input variable references poor data.
You are watching: Validation loops are also known as
One that the most famed sayings among computer programmers is "garbage in, garbage out." This saying, occasionally abbreviated together GIGO, describes the fact that computer systems cannot tell the distinction between good data and also bad data. If a user provides bad data as input to a program, the program will procedure that negative data and, together a result, will certainly produce bad data as output. For example, look in ~ the payroll program in routine 5-15 and notice what wake up in the sample run once the user gives bad data together input.
# This program displays gross pay. Def main():
# acquire the number of hours worked.
hours = input("Enter the hours functioned this week:
# obtain the hourly pay rate.
pay-rate = input("Enter the hourly pay rate: ")
# calculation the gross pay. Gross-pay = hours * pay-rate
# display the pistol pay. Publish "The gross pay is
# speak to the key function. That main()
Program calculation (with input displayed in bold)
Enter the hours functioned this week: 400
Did you spot the poor data the was noted as input? The human receiving the paycheck will certainly be pleasantly surprised, since in the sample operation the payroll clerk entered 400 together the variety of hours worked. The clerk more than likely meant to get in 40, because there space not 400 hours in a week. The computer, however, is unaware that this fact, and also the routine processed the bad data simply as if that were good data. Deserve to you think that other varieties of entry that deserve to be given to this routine that will result in poor output? One example is a negative number gotten in for the hours worked; an additional is one invalid hourly salary rate.
Sometimes stories room reported in the news about computer errors that mistakenly cause people to be charged countless dollars for little parchases or come receive huge tax refunds that they were not entitled to. These "computer errors" are rarely brought about by the computer, however; lock are much more commonly caused by poor data the was read right into a regime as input.
The verity of a program"s output is only as great as the verity of the input. Because that this reason, friend should style your program in together a way that negative input is never ever accepted. When input is provided to a program, it need to be inspected before it is processed. If the input is invalid, the program should discard it and also prompt the user to get in the exactly data. This procedure is known as intake validation.
Figure 5-8 reflects a common technique for validating an item of input. In this technique, the entry is read, and then a loop is executed. If the entry data is bad, the loop executes its block the statements. The loop display screens an error post so the user will understand that the input was invalid, and also then it reads the brand-new input. The loop repeats as long as the intake is bad.
Figure 5-8 reasonable containing an entry validation loop
Figure 5-8 logic containing an intake validation loop
Notice that the flowchart in number 5-8 reads entry in 2 places: first just prior to the loop and then within the loop. The very first input operation—just before the loop — is called a priming read, and also its function is to get the very first input worth that will certainly be experiment by the validation loop. If that worth is invalid, the loop will carry out subsequent input operations.
Let"s consider an example. Expect you are designing a regimen that reads a check score and you desire to make certain the user go not go into a value less than 0. The adhering to code shows just how you deserve to use an entry validation loop come reject any kind of input worth that is less than 0.
# acquire a test score.
# Make sure it is not much less than 0. While score 100:
print "ERROR: The score cannot be negative"
print "or greater than 100.1
score = input("Enter the exactly score: ")
The loop in this code determines even if it is score is less than 0 or better than 100. If one of two people is true, an error article is displayed and the user is triggered to get in a exactly score.
See more: Elements In Group 2A(2) Of The Periodic Table Form Ions With A Charge Of
Samantha own an import business and she calculates the sleeve prices that her assets with the complying with formula:
retail price — wholesale price X 2.5 She currently uses the program shown in regime 5-16 to calculate sleeve prices.
print "ERROR: The score cannot be negative.1 score = input ("Enter the correct score: ")