In: Computer Science
Python
Write a 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).
1.
'''
Python version : 2.7
Python program to input the firstname, lastname and exam result of customers and output to file customers.txt
'''
# input of number of data to input
n = int(raw_input('Enter the number of records to input : '))
# open file in write mode
file = open('customers.txt','w')
i=0
# loop to input n customer data
while (i < n):
print('Enter details for customer %d ' %(i+1))
# input firstname
fname = raw_input('Enter first name : ')
# validate first name
if(fname.isalpha() and len(fname) <= 20):
# input last name
lname = raw_input('Enter last name : ')
# validate last name
if(lname.isalpha() and len(lname) <= 20):
try:
# input exam result
result = int(raw_input('Exam result (an integer between 1 and 100 inclusive) : '))
# validate exam result's range
if(result > 0 and result <=100):
# if all valid values write to file
if(i == 0):
file.write(lname+' '+fname+' '+str(result))
else:
file.write('\n'+lname+' '+fname+' '+str(result))
i = i + 1
# invalid exam result
else:
print('Value of result : '+str(result) +' is invalid')
except ValueError as e: # invalid exam result
print('Value of result : '+str(e)+' is invalid')
else: # invalid last name
print('Value of last name : '+lname +' is invalid')
else: # invalid first name
print('Value of first name : '+fname +' is invalid')
#close the file
file.close()
#end of program
Code Screenshot:
Output:
File:
2.
'''
Python version : 2.7
Python program to read firstname , lastname and exam result from customers.txt and validate the fields
and output valid customers and for invalid customers ,
output the reason for the record being invalid and the customer data
'''
# open the input file
file = open('customers.txt','r')
# read a line from file
line = file.readline()
# read till end of file
while(line):
try:
# split the data read from file using space as the delimiter
# and strip is used to remove any trailing or leading whitespace present in line
data = line.strip().split()
# validate the number of fields read
if(len(data) == 3):
# validate lastname
if(data[0].isalpha() and len(data[0]) <= 20):
# validate firstname
if(data[1].isalpha() and len(data[1]) <= 20):
result = int(data[2])
# validate exam result
if result > 0 and result <=100:
print(line.strip())
else:
raise ValueError('Value of result : '+str(result)+' is invalid')
else:
raise ValueError('Value of first name : '+data[1]+' is invalid')
else:
raise ValueError('Value of last name : '+data[0]+' is invalid')
else:
raise ValueError('There should be exactly 3 fields')
line = file.readline()
except ValueError as e:
print(e)
print(line)
#close the file
file.close()
#end of program
Code Screenshot:
Output:
Input file:
Output: