Python 我如何添加try/除了性别拼写错误的情况?

Python 我如何添加try/除了性别拼写错误的情况?,python,python-2.7,error-handling,Python,Python 2.7,Error Handling,我需要添加一个try/除了用户拼错单词“男”或“女”的情况 下面是我现在在代码中的用法,但我的任务要求我加入try/except,以避免拼写错误的“男”或“女”。我使用的以下try/except代码帮助解决了用户输入年龄单词而不是性别拼写错误的问题 感谢您的帮助 fem_names = [] fem_ages = [] male_names = [] male_ages = [] while True: try:

我需要添加一个try/除了用户拼错单词“男”或“女”的情况

下面是我现在在代码中的用法,但我的任务要求我加入try/except,以避免拼写错误的“男”或“女”。我使用的以下try/except代码帮助解决了用户输入年龄单词而不是性别拼写错误的问题

感谢您的帮助

fem_names = []
fem_ages = []
male_names = []
male_ages = []


while True:
  try:                                               
    name = raw_input('Enter name:')                             
    age = raw_input('Enter age:')                               
    sex = raw_input('Enter female or male:')                    

    if (sex == 'female'):                                       
        fem_names.append(name)                                  
        fem_ages.append(float(age))                             

    else:                                                       
        male_names.append(name)                                 
        male_ages.append(float(age))                            
    reply = raw_input('Type yes to continue or no to stop:')    
    if (reply == "no"):                                         
        break                                                   
  except Exception:
    print('Please enter correct information')

tot_fem = len(fem_names)                                                                        
tot_fem_ages = sum(fem_ages)                                    
if(tot_fem == 0):                                                
  avg_fem_age = 0.0                                             

else:                                                           
  avg_fem_age = tot_fem_ages/tot_fem       




tot_male = len(male_names)                                      
tot_male_ages = sum(male_ages)                                  
if(tot_male == 0):                                              
  avg_male_age = 0.0                                            

else:                                                           
  avg_male_age = tot_male_ages/tot_male                                          


print "There are", tot_fem , "total females with an average age of" , avg_fem_age, "years."             
print "There are", tot_male , "total males with an average age of" , avg_male_age, "years." 

你可以先用“如果”检查“性”

sex = raw_input('Enter female or male:')                    
if sex in ['male', 'female']:
    if (sex == 'female'):                                       
        fem_names.append(name)                                  
        fem_ages.append(float(age))                             

    else:                                                       
        male_names.append(name)                                 
        male_ages.append(float(age)) 
else:
       # raise error here
       print "invalid sex"       

你应该从阅读开始。分享你的语法错误。它不应该是
except
,而不是
except
except
语句(注意大小写)仅作为
try
语句的一部分有效。而且,他们捕捉错误;你想抓住什么错误?仅输入“男性”或“女性”以外的内容并不是错误。您的意思不是“添加异常”,而是“为
性别
无效的情况添加错误处理”。您可以使用正常的控制流,如
if-then-else、break、continue
,而不是引发异常。