Question: I am trying to add an try/except in the code to allow the user to misspell the words Male and Female; add an exception for
"""I am trying to add an try/except in the code to allow the user to misspell the words "Male" and "Female"; add an exception for dealing with this issue and create a message to the user to reenter the data. Repeat the process for ages entered outside of the 1865 range."""
# These are the databases that are needed to create the program
data = [] # Creates a list to store ALL data maleData = [] # Creates a sublist to store male records femaleData = [] # Creates a sublist to store female records
# This is the boolean function that allows the program to be ended
cont = True
while cont: # This is the while true statement, which allows the name, age, # and gender to be input in a loop until the condition is false. name = raw_input("Enter name > ") # Name prompt age = int(raw_input("Enter age > ")) # Age prompt gender = raw_input ("Enter gender(Male/Female) > ").lower() #Gender prompt cont=raw_input("Do you wish to continue?(Yes/No) > ").lower()# This is the loop that allows for the program to end with a "Y" or an "N" if cont == 'Yes': print "" elif cont == "No": cont = False
data.append ([name, age, gender])# Appends new record to data
maleAgeSum = 0 # Stores the sum of male ages femaleAgeSum = 0 # Stores the sum of female ages
# This section creates data from the above while condition.
for i in range(len(data)): # Used to write data to correct group "M" or "F" if data[i][2]=='Male': # Append male data with age maleData.append(data[i]) # Appends to the male data maleAgeSum += data[i][1] # Stores Male age sum elif data[i][2]=='Female': # Append female and age femaleData.append(data[i]) #Append female data with age femaleAgeSum += data[i][1]# Stores female age sum
if len(maleData):# Find male average maleAvgAge = int(maleAgeSum)/int(len(maleData)) #Tells the computer to use the male sum / collected data else: maleAverageAge = 0 # If not males recorded, then 0 is entered.
if len(femaleData): # Find female average femaleAvgAge = int(femaleAgeSum)/int(len(femaleData)) # Tells Python to use the female sum / collected data else: femaleAvgAge = 0 # If no females recorded, then 0 is entered.
print "Average male age is " + str(maleAvgAge) # Prints male average
print"NAME AGE GENDER > "
if len(maleData) == 0: print "No records to display." # If no data for i in range(len(maleData)): # Used to display all the data in [i][j] i.e. name, age, gender for j in range(len(maleData[i])): print str(maleData[i][j]) + '\t' , # Add tab between data, comma makes print horizontal print "" # Prints the data from the Male list and puts a tab in
print "Average female age is " + str(femaleAvgAge)# print female average
print "NAME AGE GENDER > "
if len(femaleData) == 0: print "No records to display." # If no data for i in range(len(femaleData)): # Used to display all the data in [i][j] i.e. name, age, gender for j in range(len(femaleData[i])): print str(femaleData[i][j]) + '\t', # Add tab between data, comma makes print horizontal print ""
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
