python中的最后一个else子句不起作用

时间:2015-12-21 14:03:29

标签: python

当我运行程序并输入性别作为其他任何东西但不是男性或女性时,程序无法按预期工作。我已经挣扎了一天,无法弄清楚。另外如何在调用之后放置函数并仍然使程序运行?我读了一个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()

1 个答案:

答案 0 :(得分:5)

改变这个:

elif gender == 'female' or 'Female':

到此:

elif gender in ('female', 'Female'):

或者我会建议:

elif gender.lower()=='female':

你拥有什么

elif gender == 'female' or 'Female':
如果性别是女性,或者字符串"女性"

基本上是。是非空的。无论gender的价值如何,这都将是真实的。