Question

In: Computer Science

Write a python program that loops, prompting the user for their full name, their exam result...

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:

  • First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long.
  • Exam result (an integer between 1 and 100 inclusive)

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.

Solutions

Expert Solution

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()


Related Solutions

Python Write a program that loops, prompting the user for their full name, their exam result...
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: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
Write a program that loops, prompting the user for their full name, their exam result (an...
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: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record each...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
In.java Write down a program that asks the user for their full name given in the...
In.java Write down a program that asks the user for their full name given in the format first last. The program will do the necessary processing and print the name in a table with 3 columns: Last, First, Initials. Requirements/Specifications... Your program run must be identical with the one shown as an example (both in how it reads the input and it on what it prints). The table must have the top and bottom lines as shown. The columns for...
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
Python Code: Write a program to prompt the user to enter a first name, last name,...
Python Code: Write a program to prompt the user to enter a first name, last name, student ID, number of hours completed and GPA for 5 students. Use a loop to accept the data listed below. Save the records in a text file. Write a second program that reads the records from your new text file, then determines if the student qualifies for Dean’s list or probation. Display the records read from the file on screen with appropriate headings above...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT