Question

Need help with the code for this assignment. Python Assignment: List and Tuples We will do...

Need help with the code for this assignment.

Python Assignment: List and Tuples

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

GirNames.txt
contains a list of the 200 most popular names given to girls born in US from year 2000 thru 2009: (copy link and create a txt file): https://docs.google.com/document/d/11YCVqVTrzqQgp2xJqyqPruGtlyxs2X3DFWn1YUb3ddw/edit?usp=sharing

BoyNames.txt
contains a list of the 200 most popular names given to boys born in US from year 2000 thru 2009 (copy link and create txt file): https://docs.google.com/document/d/12XrZdpMxhs16PZ_fmCcR_5xEE5PDHd1XFNmAfZUWyvU/edit?usp=sharing

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

Note: We will test code with THREE sample runs:

  • First run uses the names Michael and Emma (see sample run below)
  • 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

Sample Output:

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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#try and except for IOerror
try:
fboys =open("boynames.txt",'r');
except:
print("failed to open boynames.txt");
  
try:
fgirls =open("girlnames.txt",'r');
except:
print("failed to open girlnames.txt");

#lines read to list   
boysPopular=fboys.readlines();
length=len(boysPopular); #length of list
i=0;
#strip and use lower case to save names
while i<length:
boysPopular[i]=boysPopular[i].strip().lower();
i+=1;
  
fboys.close()
#lines read to list   
girlsPopular=fgirls.readlines();
length=len(girlsPopular);
i=0;
#strip and use lower case to save names
while i<length:
girlsPopular[i]=girlsPopular[i].strip().lower();
i+=1;

fgirls.close();

#get name and store in lower case for comparison
boyName=input("Enter a boy's name, or N if you do not wish to enter a boy's name: ").lower();
if boyName != 'n':
if boyName in boysPopular:
#capitalize for better viewing
print(boyName.capitalize(),"is among the most popular boys name.");
else:
print(boyName.capitalize(),"is not among the most popular boys name.");

#get name and store in lower case for comparison
girlName=input("Enter a girl's name, or N if you do not wish to enter a girl's name: ").lower();
if girlName != 'n':
#capitalize for better viewing
if girlName in girlsPopular:
print(girlName.capitalize(),"is among the most popular girls name.");
else:
print(girlName.capitalize(),"is not among the most popular girls name.");
  
  
  

  9 #try and except for IOerror 10 try: 11 fboys =open(boynames.txt,r); 12 except: 13 print(failed to open boynames.txt);

Enter a boys name, or N if you do not wish to enter a boys name: Michael Michael is among the most popular boys name. Enter

Enter a boys name, or N if you do not wish to enter a boys name: N Enter a girls name, or N if you do not wish to enter a

Enter a boys name, or N if you do not wish to enter a boys name: John John is among the most popular boys name. Enter a gir

Thanks

Add a comment
Know the answer?
Add Answer to:
Need help with the code for this assignment. Python Assignment: List and Tuples We will do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Visual Basic Question In the Chap9 folder of the student sample programs, you will find the...

    Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 200 most popular names given to girls born in the United States from 2000 to 2012 BoyNames.txt - This file contains a list of the 200 most popular names given to boys born in the United States from 2000 to 2012 Create an application that reads the contents of the two files into...

  • The text files boynames.txt and girlnames.txt, which are included in the source code for this boo...

    The text files boynames.txt and girlnames.txt, which are included in the source code for this book, contain lists of the 1,000 most popular boy and girl names in the United States for the year 2005, as compiled by the Social Security Administration. These are blank-delimited files where the most popular name is listed first, the second most popular name is listed second, and so on to the 1,000th most popular name, which is listed last. Each line consists of the...

  • I need help with this question for programming. The language we use is C++ The program...

    I need help with this question for programming. The language we use is C++ The program should ask the user if they want to search for a boy or girl name. It should then ask for the name search the appropriate array for the name and display a message saying what ranking that name received, if any. The name files (BoyNames.txt and GirlNames.txt) are in order from most popular to least popular and each file contains two hundred names. The...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Python installation: Install Python 3 as described here. (We will work with Windows PC's.) Launch IDLE...

    Python installation: Install Python 3 as described here. (We will work with Windows PC's.) Launch IDLE Enter the program listed in the attached file: "First program in IDLE". Save a "ProgA.py" Debug until code runs correctly. Take a screenshot of the correct output Save as "YourLastName_IDLE1.png" Upload here How do you debug # First Python Program #Fill date here # Name: Fill name here # _________________________ first = input("Enter first name:") last = input("Enter last name:") print("Hello " + first...

  • PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK...

    PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK YOU! Bonus Question Implement an efficient algorithm (Python code) for finding the 11th largest element in a list of size n. Assume that n will be greater than 11. det find 11th largest numberlissy Given, lissy, a list of n integers, the above function will return the 11th largest integer from lissy You can assume that length of lissy will be greater than 11....

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

  • It's in Python 3. Please make sure it displays correct strings lenght And displays correct character...

    It's in Python 3. Please make sure it displays correct strings lenght And displays correct character lenght 1 def sum list(numbers): Sums a list of integers param numbers: a list of intege rs return: the sum of the integers in numbe rs def get_string_lengths (st rings) : 10 11 Given a list of strings, return a list of intege rs representing the lengths of the input strings :param strings: a list of strings return: a list of integers represent ing...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • Intro to python: Billy is writing a small script to save names he enters in to...

    Intro to python: Billy is writing a small script to save names he enters in to a file using a specified separator. He reads in names until the user enters an exclamation point indicating the end of the list of names at which point it writes the list to a file. Billy, however, is getting a strange error and isn't sure about how to fix the issue. Describe what is the error and how can you fix it? def write_list_to_file(file_path,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT