In: Computer Science
Write a python program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules:
The file should record each customers information on a single line and the output file should have the following appearance.
Nurke Fred 58
Cranium Richard 97
Write a second program that opens the ‘customers.txt’ file for reading and then reads each record, splitting it into its component fields and checking each field for validity.
The rules for validity are as in your first program, with the addition of a rule that specifies that each record must contain exactly 3 fields.
Your program should print out each valid record it reads.
The program should be able to raise an exception on invalid input, print out an error message with the line and what the error was, and continue running properly on the next line(s).
You need to develop the system by completing the following three tasks:
Task 1 -
Draw flowchart/s that present the steps of the algorithm required to perform the tasks specified.
Task 2 -
Select at least six sets of test data that will demonstrate the 'normal' operation of your program; that is, test data that will demonstrate what happens when a VALID input is entered.
Thanks for the question, here is the complete code in python for the project. Comments are included so that you can follow the code precisely.
let me know for any doubts or for any questions.
==============================================================
# function takes in a name and validate if it meets
all
# conditions , return True if all conditions are met False
otherwise
def validateName(name):
if len(name) > 20:
print('Name
should be less than 20 characters.')
return
False
for letter in
name.lower():
if
letter < 'a' or letter >
'z':
print('Name contains invalid characters.')
return False
return True
# function takes in a score as string value
# validate the score is an integer and ranges between 1 and
100
# returns True if all conditions are meet False
otherwise
def validateScore(score):
try:
result =
int(score)
if
result < 1 or result > 100:
print('Exam result should be between 1 and
100')
return False
else:
return True
except:
print('Invalid
number entered.')
return
False
# keep asking input from user until input meets all the
condition
def getName(message):
while True:
name =
input(message).lower()
if
validateName(name):
return name.title()
# keep asking valid score from user until input meets all the
condition
def getScore():
while True:
score =
input('Enter exam result: ')
if
validateScore(score):
return int(score)
# takes in the list of records user entered, the filename
# and saves the data in the file
def save_to_file(records, filename):
with open(filename,
'w') as outfile:
for
record in records:
outfile.write('{} {}
{}\n'.format(record[0],
record[1], record[2]))
print('Data written to file: {}
successfully.'.format(filename))
# reads data from file and validates the record contains valid
entries
def read_file(filename):
with open(filename,
'r') as infile:
for
line in infile.readlines():
values = line.strip().split()
if len(values) != 3:
print('ERROR:
{} contains not exactly 3 values'.format(line))
elif validateName(values[0].strip())
and validateName(values[1].strip())
and validateScore(
values[2].strip()):
print('VALID RECORD: {} {} {}'.format(values[0],
values[1], values[2]))
else:
print('ERROR: {} contains either invalid name or exam
score.'.format(line))
# main function , execution starts here
def main():
records = []
filename = 'customers.txt'
while True:
firstname =
getName('Enter first name: ')
lastname =
getName('Enter last name: ')
score = getScore()
records.append([firstname, lastname, score])
add_another =
input('Do you want to add another record (Y/N):
'.upper())
if
add_another == 'N':
break
save_to_file(records, filename)
read_file(filename)
main()