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

The last else clause in python does not work

本问题已经有最佳答案,请猛点这里访问。

当我运行程序并输入性别信息时,除了男性或女性之外,程序不能按预期工作。我挣扎了一天,都搞不清楚了。另外,如何在函数调用后放置函数,并使程序仍然运行?我读过一个主函数,但不知道如何用它修改代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
elif gender == 'female' or 'Female':

对此:

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

或者我建议:

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

你带了什么

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

如果性别是女性,或者如果字符串"女性"不是空的,则本质上是另一种情况。不管EDOCX1的值是多少(0),这总是正确的。