CSCE A101 Python Assignment 4 Solved

Original price was: $40.00.Current price is: $35.00.



5/5 - (1 vote)

Popularity Contest

Write a program that repeatedly quizzes the user about baby name popularity. The files
boynames2015.txt and girlnames2015.txt list the 1000 most popular baby boy and girl names
from 2015. Your program should randomly select a boy and girl name and ask the user which
one was more popular (based on number of births, not rank). Here is example output:
> In 2015, was the name Charlotte (1) or Logan (2) more popular (enter 1 or 2)?
> 1
> Incorrect. There were 11332 girls named Charlotte, and 12862 boys named Logan
> Play again (y/n)?

To implement this, read in the data in the files using relative pathnames (this will make grading
easier). Be sure to use good file handling code (i.e. closing the file, try/except blocks).
You will want to group the rank, name, and number of births together for each name. The best
way to do this is to define a class, but since this introductory course does not place an emphasis
on classes, you may solve this problem using a list of tuples. This means you will need to create
2 lists (one for boys, one for girls) each of length 1000, and each tuple in it will be of length 3.
Then, you can randomly select indices from each list and compare the number of boy and girl
births stored at each index.

For full credit, be sure to handle invalid input for both the Play again prompt and the quiz
question prompt. For your while loops, use good, readable coding practices (no break or
continue statements, no while True logic).
Lastly, print a final message that shows the percentage of questions answered correctly.

Optional Bonus: up to 5 points

Create a class that defines a rank, name, and number of births for a single name. You may put
the class definition in your main code file for partial bonus points, but it is generally better to
place it in a separate file. If you choose to do that, you will need to import the class definition
into your main file.

Then, as you read in each line of the baby names files, create a new instance
of your class and add it to a list (one list for boys and one for girls). So instead of having a list of
tuples, you will have a list of class objects for the baby name class that you create.
What to submit? The 2 names .txt files and your python .py file(s) as a single .zip file.