在Python中的条件语句中使用“或”:“if x ==”y“或”z“”

时间:2013-03-24 10:56:01

标签: python boolean conditional

def shapearea():
    shape = raw_input("What shape do you want to print? ")
    if shape == "triangle" or "Triangle":
        return trianglearea()

    elif shape == "circle" or "Circle":
        return circlearea()

    elif shape == "square" or "Square":
        return squarearea()
    else:
        shapearea()

shapearea()

使用此代码会破坏我的程序,如何让程序注册(例如):

“circle”或“Circle”为相同的

3 个答案:

答案 0 :(得分:3)

if shape in ("triangle", "Triangle")

或更好,

if shape.lower() == "triangle"

答案 1 :(得分:1)

@ jamylak答案的替代方案,以下是您的需求:

if shape == "triangle" or shape == "Triangle":
    return trianglearea()

elif shape == "circle" or shape == "Circle":
    return circlearea()

elif shape == "square" or shape == "Square":
    return squarearea()

答案 2 :(得分:0)

So the actual function should be:  
def shapearea():
shape = raw_input("What shape do you want to print? ")
        if shape == "triangle" or shape == "Triangle":
            return trianglearea()

    elif shape == "circle" or shape == "Circle":
        return circlearea()

    elif shape == "square" or shape == "Square":
        return squarearea()
    else:
        shapearea()

shapearea()