Trying Add Try Except Code Allow User Misspell Words Male Female Add Exception Dealing Iss Q26369084

“””I am trying to add an try/except in the code to allow theuser to misspell the words “Male” and “Female”; add an exceptionfor dealing with this issue and create a message to the user toreenter the data. Repeat the process for ages entered outside ofthe 18–65 range.”””

# These are the databases that are needed to create theprogram

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 beended

cont = True

while cont: # This is the while true statement, which allows thename, age,
# and gender to be input in a loop until the condition isfalse.
  
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” oran “N”
if cont == ‘Yes’:
print “”
elif cont == “No”:
cont = False

data.append ([name, age, gender])# Appends new record todata

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 computerto 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)) # TellsPython 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 maleaverage

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, commamakes print horizontal
print “” # Prints the data from the Male list and puts a tab in

  
print “Average female age is ” + str(femaleAvgAge)# print femaleaverage

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, commamakes print horizontal
print “”

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *