当我运行程序并输入性别作为其他任何东西但不是男性或女性时,该程序不能按预期工作.我已经挣扎了一天,无法弄清楚.另外如何在调用之后放置函数并仍然使程序运行?我读了一个main函数,但无法弄清楚如何用它修改代码.
from sys import exit birthdays = {'Alice': 'April 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'} def dateOfBirth(): bday = raw_input("> ") birthdays[name] = bday print "Database records updated." print "The number of records is now " + str(len(birthdays)) + "." print birthday`enter code here`s exit(0) while True: print "Enter a name: (blank to quit)" name = raw_input("> ") if name == '': exit(0) if name in birthdays: print birthdays[name] + " is the birthday of " + name + "." exit(0) else: print "I do not have the birthday information for " + name + "." print " What is the sex?" gender = raw_input("> ") if gender == 'male': print "What is his birthday?" dateOfBirth() elif gender == 'female' or 'Female': print "What is her birthday?" dateOfBirth() else: print " The gender is not recognised. Anyway we will continue." dateOfBirth()
khelwood.. 5
改变这个:
elif gender == 'female' or 'Female':
对此:
elif gender in ('female', 'Female'):
或者我会建议:
elif gender.lower()=='female':
你有什么
elif gender == 'female' or 'Female':
如果性别是女性,或者字符串"女性"是非空的,则基本上是其他的.无论价值如何,这都将是真实的gender
.
改变这个:
elif gender == 'female' or 'Female':
对此:
elif gender in ('female', 'Female'):
或者我会建议:
elif gender.lower()=='female':
你有什么
elif gender == 'female' or 'Female':
如果性别是女性,或者字符串"女性"是非空的,则基本上是其他的.无论价值如何,这都将是真实的gender
.