Question

In: Computer Science

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

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.

Solutions

Expert Solution

# Using the newer with construct to close the file automatically.
# Opening the files and splitting by newline and storing into list
with open(boyfilename) as b:
boylist = b.read().splitlines()

with open(girlfilename) as g:
girllist = g.read().splitlines()

return boylist,girllist
except:
print('Files not found!!!')
return


def main():
# Calling loadnames method will load names into two lists
boynamelist,girlnamelist=loadnames("boynames2016.txt","girlnames2016.txt")

# taking input from users and searching in list using index method of list
while True:
gender = raw_input("Enter Gender (boy/girl): ")
name=raw_input("Enter the name to search for: ")
try:
if gender=="boy":
rank=boynamelist.index(name)+1
print name, " was ranked #", rank," in 2016 for boy names."
elif gender=="girl":
rank = girlnamelist.index(name) + 1
print name, " was ranked #" ,rank, "in 2016 for girl names."

except:
print name+" was not ranked in the top 100 "+ gender+" names for 2016"

# Exit program when 0 pressed
status=raw_input("Press 0 to Exit/Any other key to continue")
if status=="0":
break;
main()


Related Solutions

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...
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...
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..
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,  ...
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.  ...
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
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...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT