If you have downloaded the source code from this book\'s companion web site, you
ID: 3598644 • Letter: I
Question
If you have downloaded the source code from this book's companion web site, you will find the following files in the Chapter 07 folder GiriNames.txt--This file contains a list of the 200 most popular names given to girls born in the United States from the year 2000 through 2009. . BoyNames.txt--This file contains a list of the 200 most popular names given to boys born in the United States from the year 2000 through 2009. Write a program that reads the contents of the two files into two separate lists, allows a user to input either a girl's name, a boy's name, or both, then tells the user whether the name(s) was/were popular between 2000 and 2009. First, the program should prompt the user to choose a girl's name, a boy's name, or both by entering either 'girl', 'boy', or 'both. Once they have chosen, they should be able to input a name. If the name was a popular name, like Jacob or Sophia, the program should print Jacob was a popular boy's name between 2000 and 2009." or Sophia was a popular girl's name between 2000 and 2009. If the name was not a popular name, like Voldemort, the program should print Voldemort was not a popular boy's name between 2000 and 2009. If the user chooses to input both a girl and boy's name, ask for the boy's name, then the girl's name, and print two statements in the form mentioned above on two separate lines, with the statement about the boy's name coming first. For example, if the user inputs Voldemort and then Sophia, print: Voldemort was not a popular boy's name between 2000 and 2009. Sophia was a popular girl's name between 2000 and 2009. SAMPLE RUN #3: python3 Nanesearch.py Interactive SessionHide Invisibles Highlight: 11 Enter- 'boy,. 'girl', .or.'both' :girl. Enter-a.girl's name: Emmae Emma-was-a.popular-girl's-name between-2800 and-2009.Explanation / Answer
# list containg boys and grils name read from file
girlList = []
boyList = []
with open("GirlNames.txt") as fh:
for name in fh:
name = name.rstrip(" ")
girlList.append(name)
with open("BoyNames.txt") as fh:
for name in fh:
name = name.rstrip(" ")
boyList.append(name)
def print_girl_name_popularity(girlName):
if girlName in girlList:
print(girlName + " was a popular girl's name between 2000 and 2009")
else:
print(girlName + " was not a popular girl's name between 2000 and 2009")
def print_boy_name_popularity(boyName):
if boyName in boyList:
print(boyName + " was a popular boy's name between 2000 and 2009")
else:
print(boyName + " was not a popular boy's name between 2000 and 2009")
girlBoyBoth = input("Enter 'boy', 'girl', or 'both':")
if girlBoyBoth == 'girl':
girlName = input("Enter a girl's name:")
print_girl_name_popularity(girlName)
elif girlBoyBoth == 'boy':
boyName = input("Enter a boy's name:")
print_boy_name_popularity(boyName)
elif girlBoyBoth == 'both':
boyName = input("Enter a boy's name:")
girlName = input("Enter a girl's name:")
print_boy_name_popularity(boyName)
print_girl_name_popularity(girlName)
# copy pastable code link: https://paste.ee/p/eVc6U
'''
Sample run
$ python3 girl_boy_both_name.py
Enter 'boy', 'girl', or 'both':both
Enter a boy's name:Jacom
Enter a girl's name:Emma
Jacom was not a popular boy's name between 2000 and 2009
Emma was a popular girl's name between 2000 and 2009
$ python3 girl_boy_both_name.py
Enter 'boy', 'girl', or 'both':girl
Enter a girl's name:Emma
Emma was a popular girl's name between 2000 and 2009
$ python3 girl_boy_both_name.py
Enter 'boy', 'girl', or 'both':boy
Enter a boy's name:Jacob
Jacob was a popular boy's name between 2000 and 2009
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.