Task
In the Resources section of the Interact2 subject website there is a data file called cars.txt. This file contains details of a valid car rego numbers. Each rego number is a six-alphanumeric such as A1B2C3. (Note: Two records in the file have intentionally been “corrupted” – for testing purposes.)
Design an algorithm and use it to write a Python program that reads the contents of the data file into a list. The program should then loop, to allow a user to check various numbers against those stored in the list. The loop should terminate when an “empty” input occurs – i.e. when the user presses the [Enter] key on its own. (Obviously this last entry should NOT be checked against the list!) If the rego number input matches an element somewhere in the list, the program should display the number together with a message saying that it IS a valid rego number. If the number input does not match any element in the list, then the program should display the number and a message saying that it IS NOT a valid rego number.
Notes:
The algorithm should be written in pseudocode (structured English).
The records read from the data file should be checked and NOT placed into the list if they are something other than a 6-alphanumeric. (Display a sensible message if the file is found to have corrupt records in it.)
The numbers entered by the user should be checked to ensure they are valid alphanumeric (as distinct from valid rego numbers) and not processed further if they are not.
Your programs should use one or more functions where sensible, and be documented fully.
Use exceptions where necessary.
Specify 3 sets of test data that will demonstrate the correct ‘normal’ operation of your program. Show your test data in a table as you have done in earlier assignments.
Run your program using the test data you have selected and save the output produced in a single text file.
Submit:
1. Your algorithm and test data table.
2. The table recording your chosen test data.
3. Source code for your Python implementation.
4. Output test file demonstrating the results of using the test data.
5. Contents of the data file cars.txt read by the program.
It is important that the output listings are not edited in any way.
Submit to Turnitin. Please use the login details given in CSU Interact2 Announcements.
Rationale
Reinforce topic material related to files and exceptions.
Reinforce topic material related to lists.
Programming principles
Python Programming
1/23/2016
Student Name
Pseudo code:
BEGIN
Step 1. Enter file name
Step 2. If file exists, open the file
Step 3. Otherwise, print the message that ‘file not found’
Step 4. Read line from file, if line exist
Step 5. Split the line
Step 6. Get the rego number
Step 7. If rego number is equals to 6, print ‘valid’ message
Step 8. If rego number is not equals to 6, print ‘not valid’ message
Step 9. Go To step 4
END
Test Data
Normal Test 1
Input | Reason Selected | Expected Output | Actual Output |
cars.txt | Name of input file | File found | File Found |
Input from file | To check valid rego number | If file exists, program read the file and check valid rego number | If file exists, program read the file and check valid rego number |
Input from file | To check NOT valid rego number | If file exists, program read the file and check NOT valid rego number | If file exists, program read the file and check NOT valid rego number |
Abnormal test 1
Input | Reason Selected | Expected Output | Actual Output |
No input | Name of input file | Error message found | Error message found |
Abnormal test 2
Input | Reason Selected | Expected Output | Actual Output |
File which does not exists | Name of input file | Error message found that ‘file not found’ | Error message found that ‘file not found’ |
Coding
import string
#check validation fro rego number
def checkValidation(regoNum):
#check rego number with if condition
if(len(regoNum) == 6):
print(regoNum + “: It IS a valid rego number”)
else:
print(regoNum + “: It IS NOT a valid rego number”)
#read input file
def readInputFile():
infileName = input(‘Enter file name: ‘)
print(“”);
try:
# open the file for reading
infile = open(infileName, ‘r’)
except (FileNotFoundError, IOError):
print(“Note: File not found.”)
return
print(“Note: File found.”)
# read the file a line at a time and display
for line in infile:
#split by space
regoNum = line.split(‘ ‘, 1)[0]
#split by tab
regoNum = regoNum.split(‘\t’, 1)[0]
#split by new line
regoNum = regoNum.split(‘\n’, 1)[0]
if(len(regoNum) != 0):
checkValidation(regoNum)
#main() method where execution begin
def main():
#call readInputFile() method for read the file
readInputFile()
main ()
A1B2C3 Peter McDonald
MYREGO Mary Smith
ITC415 Katy Perry
JO12
HELLO1 Iam Botham
HE11O1 Peter Adams
ABC123 Nelson Mandela
HI5HI5 Kevin Jones
HELLO
ABC123 John White
ITC106 Alan Parker
TT63YY Anil Patel
ALAN13 Brian Lara
MYCARNO Smith Wills
Susan Sugar
87MYNO Margaret Bongs
import string
#check validation fro rego number
def checkValidation(regoNum):
#check rego number with if condition
if(len(regoNum) == 6):
print(regoNum + “: It IS a valid rego number”)
else:
print(regoNum + “: It IS NOT a valid rego number”)
#read input file
def readInputFile():
infileName = input(‘Enter file name: ‘)
print(“”);
try:
# open the file for reading
infile = open(infileName, ‘r’)
except (FileNotFoundError, IOError):
print(“Note: File not found.”)
return
print(“Note: File found.”)
# read the file a line at a time and display
for line in infile:
#split by space
regoNum = line.split(‘ ‘, 1)[0]
#split by tab
regoNum = regoNum.split(‘\t’, 1)[0]
#split by new line
regoNum = regoNum.split(‘\n’, 1)[0]
if(len(regoNum) != 0):
checkValidation(regoNum)
#main() method where execution begin
def main():
#call readInputFile() method for read the file
readInputFile()
main ()