1. Review the attached Lab 8 file closely.
  2. Work through the Lab Student document thoroughly. Disregard all references to Python and Python exercises.
  3. Labs enable and enhance your learning. Schedule time to work through the lab materials at least twice.
  4. Complete the Raptor programs for the following labs:
    1. Lab 8.2
    2. Lab 8.5 – Programming Challenge 1 – Cell Phone Minute Calculator
  5. Save the fully completed Raptor programs with the following filenames: LastName_Lab_X.rap (X = lab number).
  6. Upload files here. (Do not upload incomplete files. Seek prompt assistance to have questions answered.)

Lab 8:  Input Validation

This lab accompanies Chapter 7 of Starting Out with Programming Logic & Design.

Name:  ___________________________

Lab 8.1 – Input Validation

Critical Review   If a computer reads bad data as input, it will produce bad data as output.  Programs should be designed to reject bad data that is given as input.    Garbage in, garbage out (GIGO), refers to the fact that computers cannot tell the difference between good data and bad date.    Both numbers and strings can be validated.  

The goal of this lab is to identify potential errors with algorithms and programs.

Step 1:  Imagine a program that calls for the user to enter a password of at least 8 alphanumeric characters.  Identify at least two potential input errors.

_______________________________________________________________________

_______________________________________________________________________

Step 2:  Imagine a program that calls for the user to enter patients’ blood pressure.  Blood pressure ranges are between 50 and 230.  Identify at least two potential input errors.

_______________________________________________________________________

_______________________________________________________________________

Step 3:  Open either your Lab 5-3.rap flowchart or your Lab 5-4.py Python code.  This program allowed the user to enter in 7 days worth of bottle returns and then calculated the average.  Examine the program and identify at least two potential input errors.

_______________________________________________________________________

_______________________________________________________________________

Step 4:  Open either your Lab 6-4.rap flowchart or your Lab 6-4.py Python code.  This program allowed a teacher to enter any number of test scores and then calculated the average score.  Examine the program and identify at least two potential input errors.

_______________________________________________________________________

_______________________________________________________________________

Lab 8.2 – Input Validation and Pseudocode

Critical Review   Input validation is commonly done with a loop that iterates as long as an input variable contains bad data.  Either a posttest or a pretest loop will work.  If you want to also display an error message, use a pretest loop, otherwise, a posttest loop will work.   Functions are often used for complex validation code.  

The goal of this lab is to write input validation pseudocode.

Step 1:  Examine the following main module from Lab 5.2.  Notice that if the user enters a capital ‘Y’ the program will end since the while loop only checks for a lower case ‘y’.     

Module main ()

//Step 1: Declare variables below

     Declare Integer totalBottles = 0

     Declare Integer counter = 1

     Declare Integer todayBottles = 0

     Declare Real totalPayout

     Declare String keepGoing = ‘y’

     //Step 3: Loop to run program again

     While keepGoing == ‘y’

           //Step 2: Call functions

           getBottles(totalBottles, todayBottles, counter)

           calcPayout(totalBottles, totalPayout)

           printInfo(totalBottles, totalPayout)

Display “Do you want to run the program again? (Enter y for yes or n for no).”

Input keepGoing

           End While 

End Module

Step 2:  Write a line of code that will convert the input value to a lower case value.  (See Validating String Input, Page 264). 

________________________________________________________________________

________________________________________________________________________

Step 3:  Examine the getBottles module from the same program.  Notice the potential input error of the user entering a negative value into todayBottles.  Rewrite the module with an input validation loop inside the existing while loop that will verify that the entry into todayBottles is greater than 0.  If they enter a 0 or negative value, display an error message.  (Reference: Input Validation Loop, Page 258).

Previous Code

//getBottles module

Module getBottles(Integer totalBottles, Integer todayBottles, Integer counter)

     While counter <=7

Display “Enter number of bottles returned for the day:”

Input todayBottles

totalBottles = totalBottles + todayBottles

counter = counter + 1

     End While

End Module

Validation Code

//getBottles module

Module getBottles(Integer totalBottles, Integer todayBottles, Integer counter)

     While counter <=7

           ______________________________________________

           ______________________________________________

           ______________________________________________

______________________________________________

Display “Enter number of bottles returned for the day:”

Input todayBottles

                _______________________________________________

                _______________________________________________

totalBottles = totalBottles + todayBottles

counter = counter + 1

     End While

End Module

Step 4:  Examine the following pseudocode from Lab 6.4.  Rewrite the module with a validation loop so that no less than 2 students and no more than 30 students take the test. 

Previous Code

Module getNumber(Integer Ref number)

     Display “How many students took the test: ”

     Input number   

End Module

Validated Code

Module getNumber(Integer Ref number)

     ______________________________________________________

     ______________________________________________________

______________________________________________________

     ______________________________________________________

______________________________________________________

     ______________________________________________________

End Module

Step 5:  Examine the following pseudocode from Lab 6.4.  Rewrite the module with a validation loop so that the test score must be between 0 and 100. 

Previous Code

Module getScores(Real Ref totalScores, Integer number, Real score, Integer counter)

     For counter = 1 to number

           Display “Enter their score:”

           Input score

           Set totalScores = totalScores + score

     End For

End Module

Validated Code

Module getScores(Real Ref totalScores, Integer number, Real score, Integer counter)

     ______________________________________________________

     ______________________________________________________

______________________________________________________

     ______________________________________________________

______________________________________________________

     ______________________________________________________

End Module

Lab 8.3 – Functions and Flowcharts

Critical Review   Based on the type of loop used for validation, you may have noticed the concept of a priming read.  This is this the first input before the validation loop.    The purpose of this is to get the first input value that will be tested by the validation loop.   A priming read is used with a while loop, rather than a do-while loop.   Note:  If the programmer is asking for a particular type of input (either numeric or string), the user is free to enter something else.  This will normally cause a fatal error at some point of program execution.  Avoiding these fatal errors is beyond the scope of basic Raptor programming.  What this means is that all errors cannot be resolved using Raptor.  

This lab requires you to modify the flowchart from Lab 6-4.rap to incorporate validation loops.  Use an application such as Raptor or Visio. 

Step 1:  Start Raptor and open your flowchart from Lab 6-4.rap.  Go to File and then Save As and save your document as Lab 8-3.  The .rap file extension will be added automatically. 

Step 2:  In the main module, modify your loop condition so that the user must enter a “yes” or a “no” value.  This can be done with nested Loop symbols.  Your flowchart might look as follows: 

Step 3:  In the getNumber module, modify the code so that the input must be at least 2 or more students and no more than 30 students.  If the user enters a valid number, the program should continue.  If not, display an error message that says “Please enter a number between 2 and 30 – Try again!!”  Use a prime read in this situation.  Paste your getNumber module flowchart in the space below.

            PASTE FLOWCHART HERE

Step 4:  In the getScores module, modify the code so that the input must be between 0 and 100.  If the user enters a valid number, the program should continue.  If not, display an error message that says “Please enter a number between 0 and 100 – Try again!!”  Use  a prime read in this situation.  Paste your getScores module flowchart in the space below.

PASTE FLOWCHART HERE

Text Box: Critical Review

Numeric validation loops in Python are done by writing the types of loops you already are familiar with.  Code using a prime read might look as follows:

number = int(input('Enter a number between 1 and 10: '))
while number < 1 or number > 10:
    print ‘Please enter a number between 1 and 10’
    number = int(input(‘Enter a number between 1 and 10: ‘))

Validation code for string input uses the not keyword to check the opposite of something.  Code using a prime read might look as follows:

endProgram = input(‘Do you want to end program? (Enter no or yes): ‘)
while not (endProgram == ‘yes’ or endProgram == ‘no’):
    print ‘Please enter a yes or no’
    endProgram = input(‘Do you want to end program? (Enter no or yes): ‘)

“><strong>Lab 8.4 – Python Code and Input Validation</strong></p>



<p>The goal of this lab is to convert the Test Average program in Lab 8.3 to Python code. </p>



<p><strong>Step 1:  </strong>Start the IDLE Environment for Python.  Open your Lab6-4.py program and click on File and then Save As.  Select your location and save this file as <em>Lab</em>8-4<em>.py</em>.  Be sure to include the .py extension. </p>



<p><strong>Step 2:  </strong>Modify the documentation in the first few lines of your program to include your name, the date, and a brief description of what the program does to include validation.</p>



<p><strong>Step</strong> <strong>3</strong><strong>:  </strong>Modify the main function so that the user must enter either a ‘yes’ or ‘no’ value in order for the loop to continue.  Use a prime read and a while loop with an error message if a bad number is entered. </p>



<p><strong>Step 4:  </strong>Modify the getNumber function so that the user must enter a number between 2 and 30.  Use a prime read and a while loop with an error message if a bad number is entered. </p>



<p><strong>Step 5:  </strong>Modify the getScores function so that the user must enter a number between 0 and 100.  Use a prime read and a while loop with an error message if a bad number is entered. </p>



<p><strong>Step 6:  </strong>Execute your program so that all error code works and paste final code below:    </p>



<p><strong>PASTE CODE HERE</strong><strong><br></strong></p>



<p><strong>Lab 8.5 – Programming Challenge 1 – Cell Phone Minute Calculator</strong></p>



<p>Write the Flowchart and Python code for the following programming problem based on the pseudocode below.</p>



<p>Design and write a program that calculates and displays the number of minutes over the monthly contract minutes that a cell phone user incurred.  The program should ask the user how many minutes were used during the month and how many minutes they were allowed.  Validate the input as follows:</p>



<ul>
<li>The minimum minutes allowed should be at least 200, but not greater than 800.  Validate input so that the minutes allowed are between 200 and 800.</li>



<li>The minutes used must be over 0.  Validate input so that the user does not enter a negative value. </li>
</ul>



<p>Once correct data is entered, the program should calculate the number of minutes over the minute allowed.  If minutes were not over, print a message that they were not over the limit.  If minutes were over, for every minute over, a .20 fee should be added to the monthly contract rate of 74.99.  Be sure not to add the .20 fee for minutes 1 to the number of minutes allowed, but rather just minutes over.  Display the number of minutes used, minutes allowed, the number of minutes over, and the total due that month.</p>



<p>You might consider the following functions:</p>



<ul>
<li>A function that allows the user to enter in minutes allowed within the range of 200 and 800.</li>



<li>A function that allows the user to enter in the minutes used greater than or equal to 0.</li>



<li>A function that calculates the total due and the total minutes over.</li>



<li>A function that prints a monthly use report.</li>
</ul>



<p>Your sample output might look as follows (note the validation code):</p>



<p><strong>Sample 1 Showing Validation:</strong></p>



<p>How many minutes are allowed: 1000</p>



<p>Please enter minutes between 200 and 800</p>



<p>How many minutes are allowed: 801</p>



<p>Please enter minutes between 200 and 800</p>



<p>How many minutes are allowed: 350</p>



<p>How many minutes were used: -10</p>



<p>Please enter minutes used of at least 0</p>



<p>How many minutes were used: 400</p>



<p>You were over your minutes by  50</p>



<p>—————MONTHLY USE REPORT——————</p>



<p>Minutes allowed were 350</p>



<p>Minutes used were 400</p>



<p>Minutes over were 50</p>



<p>Total due is $ 84.99</p>



<p>Do you want to end program? (Enter no or yes): NO</p>



<p>Please enter a yes or no</p>



<p>Do you want to end program? (Enter no or yes): 9</p>



<p>Please enter a yes or no</p>



<p>Do you want to end program? (Enter no or yes): no</p>



<p><strong>Sample 2 Showing Minutes Over:</strong></p>



<p>How many minutes are allowed: 600</p>



<p>How many minutes were used: 884</p>



<p>You were over your minutes by  284</p>



<p>—————MONTHLY USE REPORT——————</p>



<p>Minutes allowed were 600</p>



<p>Minutes used were 884</p>



<p>Minutes over were 284</p>



<p>Total due is $ 131.79</p>



<p>Do you want to end program? (Enter no or yes): no</p>



<p><strong>Sample 3 Showing Minutes Not Over:</strong></p>



<p>How many minutes are allowed: 400</p>



<p>How many minutes were used: 379</p>



<p>You were not over your minutes for the month</p>



<p>—————MONTHLY USE REPORT——————</p>



<p>Minutes allowed were 400</p>



<p>Minutes used were 379</p>



<p>Minutes over were 0</p>



<p>Total due is $ 74.99</p>



<p>Do you want to end program? (Enter no or yes): yes</p>



<p><strong>The Pseudocode</strong></p>



<p>Module main()</p>



<p>            //Declare local variables</p>



<p>            Declare String endProgram = “no”</p>



<p>            While (endProgram == “no”</p>



<p>                        Declare Integer minutesAllowed = 0</p>



<p>                        Declare Integer minutesUsed = 0</p>



<p>                        Declare Real totalDue = 0</p>



<p>                        Declare Integer minutesOver = 0</p>



<p>//calls functions</p>



<p>Set minutesAllowed = getAllowed(minutesAllowed</p>



<p>Set minutesUsed = getUsed(minutesUsed</p>



<p>Set totalDue, minutesOver = calcTotal(minutesAllowed, minutesUsed, totalDue, minutesOver)</p>



<p>Call printData(minutesAllowed, minutesUsed, totalDue, minutesOver)</p>



<p>Display “Do you want to end program? yes or no”</p>



<p>Input  endProgram</p>



<p>While endProgram != “yes” or endProgram != “no”</p>



<p>            Display “Please enter yes or no”</p>



<p>            Display “Do you want to end program? yes or no”</p>



<p>            Input endProgram</p>



<p>End While</p>



<p>            End While       </p>



<p>End Module</p>



<p>Function Integer getAllowed(Integer minutesAllowed)</p>



<p>            Display “How many minutes are allowed”</p>



<p>            Input minutesAllowed</p>



<p>            While minutesAllowed < 200 OR minutesAllowed > 800</p>



<p>                        Display “Please enter minutes between 200 and 800”</p>



<p>                        Display “How many minutes are allowed”</p>



<p>                        Input minutesAllowed</p>



<p>            End While</p>



<p>            Return minutesAllowed</p>



<p>End Function</p>



<p>Function Integer getUsed(Integer minutesUsed)</p>



<p>            Display “How many minutes were used”</p>



<p>            Input minutesUsed</p>



<p>            While minutesUsed < 0</p>



<p>                        Display “Please enter minutes of at least 0”</p>



<p>                        Display “How many minutes were used”</p>



<p>                        Input minutesUsed</p>



<p>            End While</p>



<p>            Return minutesUsed</p>



<p>End Function</p>



<p>Function Real Integer calcTotal(Integer minutesAllowed, Integer minutesUsed, Real totalDue, Integer minutesOver)</p>



<p>            Real extra = 0</p>



<p>            If minutesUsed <= minutesAllowed then</p>



<p>                        Set totalDue = 74.99</p>



<p>                        Set minutesOver = 0</p>



<p>                        Display “You were not over your minutes for the month”</p>



<p>            Else</p>



<p>                        Set minutesOver = minutesUsed – minutesAllowed</p>



<p>                        Set extra = minutesOver * .20</p>



<p>                        Set totalDue = 74.99 + extra</p>



<p>                        Display “You were over your minutes by”, minutesOver</p>



<p>            End If</p>



<p>            Return totalDue, minutesOver</p>



<p>End Function</p>



<p>Module printData (Integer minutesAllowed, Integer minutesUsed, Real totalDue, Integer minutesOver)</p>



<p>            Display “—————-MONTHLY USE REPORT———————-“</p>



<p>            Display “Minutes allowed were”, minutesAllowed</p>



<p>            Display “Minutes used were”, minutesUsed</p>



<p>            Display “Minutes over were”, minutesOver</p>



<p>            Display “Total due is $”, totalDue</p>



<p><strong>The Flowchart</strong></p>



<p>            <strong>PASTE FLOWCHART HERE</strong></p>



<p><strong>The Python Code</strong></p>



<p>            <strong>PASTE CODE HERE</strong></p>



<p><strong>If you need answers to this assignment, WhatsApp +1 646 978 1313 or send us an email to </strong><a href=admin@shrewdwriters.com and we will reply instantly. We provide original answers that are not plagiarized, please try our service.

Leave a Reply

Your email address will not be published.