In: Computer Science
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:
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()
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()