Question

In: Computer Science

In Python This assignment involves the use of text files, lists, and exception handling and is...

In Python

This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment.
You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example …

>>>   Enter gender (boy/girl): boy Enter the name to search for: Michael Michael was ranked # 7 in 2014 for boy names. >>> ================================ RESTART ================================ Enter gender (boy/girl): boy Enter the name to search for: MIchAel MIchAel was ranked # 7 in 2014 for boy names. >>> ================================ RESTART ================================ Enter gender (boy/girl): boy Enter the name to search for: Jeremiah Jeremiah was ranked # 56 in 2014 for boy names. >>> ================================ RESTART ================================ Enter gender (boy/girl): girl Enter the name to search for: olivia olivia was ranked # 2 in 2014 for girl names. >>> ================================ RESTART ================================ Enter gender (boy/girl): girl Enter the name to search for: billie jean billie jean was not ranked in the top 100 girl names for 2014. >>> ================================ RESTART ================================ Enter gender (boy/girl): gril Enter the name to search for: sue Invalid gender >>>

Make sure to use try/except blocks to handle the exception if the files are not found or unavailable.   
Use the index method on the list to find the baby name. Use a try/except block to handle the exception caused when the name is not found in the file.  
Also, check for an invalid gender entry.
Make sure to include header comments and function comments in your program.  

Solutions

Expert Solution

def main():
    boy_names = []
    girl_names = []
    try:
        with open('boynames2014.txt', 'r') as f:
            for name in f:
                boy_names.append(name.strip())
    except FileNotFoundError:
        print('Error. Can not read from boynames2014.txt')
        return None
    try:
        with open('girlnames2014.txt', 'r') as f:
            for name in f:
                girl_names.append(name.strip())
    except FileNotFoundError:
        print('Error. Can not read from girlnames2014.txt')
        return None

    gender = input('Enter gender (boy/girl): ')

    if gender == 'boy':
        lst = boy_names
    elif gender == 'girl':
        lst = girl_names
    else:
        print('Invalid gender')
        return None

    name = input('Enter the name to search for: ')
    try:
        index = lst.index(lst)
        print('%s was ranked #%d in 2014 for %s names.' % (name, index+1, gender))
    except ValueError:
        print('%s was not ranked in the top 100 %s names for 2014.' % (name, gender))

main()

Related Solutions

This assignment involves the use of text files, lists, and exception handling and is a continuation...
This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>> Enter gender (boy/girl): boy...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
USE BASIC PYTHON Focus on Basic file operations, exception handling. Save a copy of the file...
USE BASIC PYTHON Focus on Basic file operations, exception handling. Save a copy of the file module6data.txt (Below) Write a program that will a. Open the file module6data.txt b. Create a second file named processed. txt c. Read the numbers from the first file one at a time. For each number write into second file, if possible, its I. Square ii. Square root iii. Reciprocal (1/number)use a math module function for the square root. use exception handling to trap possible...
PYTHON...... Working with lists, functions, and files Objective: Work with lists Work with lists in functions...
PYTHON...... Working with lists, functions, and files Objective: Work with lists Work with lists in functions Work with files Assignment: Part 1: Write a program to create a text file which contains a sequence of test scores. Ask for scores until the user enters an empty value. This will require you to have the scores until the user enters -1. After the scores have been entered, ask the user to enter a file name. If the user doesn’t not enter...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string as a parameter and returns an integer constructed out of the digits that appear in the string. The digits in the integer should appear in the same order as the digits in the string. If the string does not contain any digits or an empty string is provided as a parameter, the value 0 should be return.
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each...
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each of the following functions. The function header must be implemented exactly as specified. Write a main function that tests each of your functions. Specifics In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data...
in C#, What is an exception and what are the advantages of exception handling?
in C#, What is an exception and what are the advantages of exception handling? What are the differences between the traditional error-handling methods and the object-oriented exception-handling methods and when should you use each one? Provide three to four paragraphs detailing your findings/views on these questions..provide examples with a simple code also..
Starting out with Python Files and Exceptions - Random Number Writer, Reader, and Exception Handler In...
Starting out with Python Files and Exceptions - Random Number Writer, Reader, and Exception Handler In this assignment, you will be writing a series of random numbers to a file, reading the file, and handling exceptions. Begin by writing a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 100. The application should let the user specify how many random numbers the file will hold. Next, write...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT