Question

In: Computer Science

We will do some basic data analysis on information stored in external files. You will find...

We will do some basic data analysis on information stored in external files.

You will find the following data files in the Source Code -> Chapter 7 folder you should have downloaded already downloaded/unzipped in Lesson 3. If you need that link again:

Pyton4E_Source_Code.zip

GirNames.txt
contains a list of the 200 most popular names given to girls born in US from year 2000 thru 2009

BoyNames.txt
contains a list of the 200 most popular names given to boys born in US from year 2000 thru 2009

Hint: See Program 7-15 read_list.py for example of reading a file's contents into a list and using a loop to strip each \n

Here is the high-level algorithm (you still have work to do :)

open BoyNames.text for reading
read all lines into a list
close the file

while there are elements in list
strip the \n from each element

open GirlNames.txt for reading
read all lines into a list
close file
while there are elements in list
strip \n from each element
get user input for boy
get user input for girl
display result for boy's name entered by user (you need a decision structure as output depends on user input)
display result for girl's name entered by user (you need a decision structure as output depends on user input)

Input: a boy's name, a girl's name, or 'N' for none
Output: messages indicating whether the names were among the most popular

Note: Use a try/except to catch IOError
as anytime we deal with external files, problems may occur (file doesn't exist, we don't have permissions, disk is corrupted, and so on).


Sample Input/Output

IMPORTANT NOTE: The external files MUST be in same directory as your source code.

You may run your code on the command line/system prompt (as I have done) OR from IDLE (Run->Run Module or F5).

Here is first run showing output when user input for both boy and girl are popular names (user input shown in italics/bold ).

Enter a boy's name, or N if you do not wish to enter a boy's name: Michael
Enter a girl's name, or N if you do not wish to enter a girl's name: Emma
Michael is one of the most popular boy's names.
Emma is one of the most popular girl's names.

Here is a second run where user says 'N' to entering names

$ python babyname.py
Enter a boy's name, or N if you do not wish to enter a boy's name: N
Enter a girl's name, or N if you do not wish to enter a girl's name: N
You chose not to enter a boy's name.
You chose not to enter a girl's name.


Here is third run showing user input with a girl name not in the GirlNames.txt file:

$ python babyname.py
Enter a boy's name, or N if you do not wish to enter a boy's name: John
Enter a girl's name, or N if you do not wish to enter a girl's name: Ada
John is one of the most popular boy's names.
Ada is not one of the most popular girl's names.

Note: We will test code with THREE sample runs:

  • First run uses the names Michael and Emma (see sample run above)
  • Second run user enters N for both of the prompts (answer N for boys and N for girls)
  • Third run we enter names of our choice but at least one is NOT popular.

this is what i did for this assignment but i got some error could you please help me with it

def main():
#open BoyNames.text for reading
infile = open('BoyNames.text', 'r')
#read all lines into a list
BoyNames = infile.readlines()
#close the file
infile.close()
#while there are elements in list
#strip the \n from each element
index = 0
while index < len(BoyNames):
BoyNames[index] = BoyNames[index].rstrip('\n')
index += 1
  

#open GirlNames.txt for reading
infile = open('GirlNames.txt', 'r')
#read all lines into a list
GirlNames = infile.readlines()
#close the file
infile.close()
# while there are elements in list
# strip \n from each element
index = 0
while index < len(GirlNames):
GirlNames[index] = GirlNames[index].rstrip('\n')
index += 1
  
#get user input for boy
try:
BoyName = input ("Enter a boy's name, or N if you do not wish to enter a boy's name:")
print (BoyNames)

GirlName = input ("Enter a gilr's name, or N if you do not wish to enter a girl's name:")
print (GirlNames)

except IOError:
print ("You chose not to enter a girl's name.")

  

main()

Solutions

Expert Solution

Here is the Python code:

def main():
    #open BoyNames.text for reading
    infile = open('BoyNames.txt', 'r')
    #read all lines into a list
    BoyNames = infile.readlines()
    #close the file
    infile.close()
    #while there are elements in list
    #strip the \n from each element
    index = 0
    while index < len(BoyNames):
        BoyNames[index] = BoyNames[index].rstrip().upper()
        index += 1
 
    #open GirlNames.txt for reading
    infile = open('GirlNames.txt', 'r')
    #read all lines into a list
    GirlNames = infile.readlines()
    #close the file
    infile.close()
    # while there are elements in list
    # strip \n from each element
    index = 0
    while index < len(GirlNames):
        GirlNames[index] = GirlNames[index].rstrip().upper()
        index += 1
      
    #get user input for boy
    try:
        BoyName = input ("Enter a boy's name, or N if you do not wish to enter a boy's name:")
        GirlName = input ("Enter a girl's name, or N if you do not wish to enter a girl's name:")

        if BoyName.upper() == "N":
            print ("You chose not to enter a boy's name.")
        elif BoyName.upper() in BoyNames:
            print ("%s is one of the most popular boy's names" %BoyName)
        else:
            print ("%s is not one of the most popular boy's names" %BoyName)

        if GirlName.upper() == "N":
            print ("You chose not to enter a girl's name.")
        elif GirlName.upper() in GirlNames:
            print ("%s is one of the most popular girl's names" %GirlName)
        else:
            print ("%s is not one of the most popular girl's names" %GirlName)

    except IOError:
        print ("Some error occurred.")

main()

Related Solutions

In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
1. Can we find the information about recently opened files from registry data? If yes, where...
1. Can we find the information about recently opened files from registry data? If yes, where is the location? 2. Can we find the information about recently typed URL list by the user from registry data? If yes, where is the location?
We have a lot of data and information. If you want to forecast something, find data...
We have a lot of data and information. If you want to forecast something, find data for it from the library. Let us call this data Dependent variable . Also find data for variables,( let us call them Independent Variables) that influence dependent variables. Your task is to find data for one dependent variable and more than one independent variables. The independent variables must be related to the dependent variable. Using your data,  run the regression on Excel and comment...
How do you determine if the data/information you are using for an analysis is reliable and...
How do you determine if the data/information you are using for an analysis is reliable and valid? (You'll have to consider the definitions of reliability and validity.) - How critical is this type of concern for the Health Services Area?
You are hired to do some data analysis for a financial services company. There are three...
You are hired to do some data analysis for a financial services company. There are three different Investment Plans that the company could offer to its customers. You are asked to find out if the expected revenue from marketing any one of these plans is different from the other plans. You survey 60 customers and this survey asks each customer to choose a plan they prefer and also involves other relevant questions that allow you to compute the expected revenues...
Do a Google search for any term involving medigap. Share some information you find. Do you...
Do a Google search for any term involving medigap. Share some information you find. Do you think consumers are savvy regarding medigap insurance or is just another confusing part to the Medicare puzzle which many do not understand?
Do you believe we should enact some form of Universal Basic Income (UBI) for all citizens...
Do you believe we should enact some form of Universal Basic Income (UBI) for all citizens or residents? Why or why not? subject business ethics
 In this exercise, you will create two external style sheet files and a web page. You...
 In this exercise, you will create two external style sheet files and a web page. You will experiment with linking the web page to the external style sheets and note how the display of the page is changed. Create an external style sheet (call it format1.css) to format as follows: document background color of white, document text color of #000099, and document font family of Arial, Helvetica, or sans-serif. Hyperlinks should have a background color of gray (#CCCCCC). Configure the...
Why is the analysis of historical accounting information relevant when we do valuations?
Why is the analysis of historical accounting information relevant when we do valuations?
How methods of data collection and analysis jeopardise external validity ?
How methods of data collection and analysis jeopardise external validity ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT