-3
我需要添加一個嘗試/除非用戶拼錯單詞「男」或「女」。如何添加try /除了拼寫錯誤的情況?
下面是我現在的代碼,但我的任務要求我加入try /除了拼錯「Male」或「Female」。下面的嘗試/除了我使用的代碼有助於解決用戶輸入年齡單詞的問題,而不是他們的性別拼寫錯誤。
任何幫助表示讚賞。
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."
你應該開始的 '性' errors.html)。 – BrenBarn
分享你的語法錯誤。 – kenorb
它不應該是'除了',而不是'除外'? – Sam