在Python条件语句中使用"或":"if x == "y" or "z

0 投票
3 回答
2829 浏览
提问于 2025-04-17 20:07
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

在编程中,有时候我们会遇到一些问题,可能是因为代码写得不够清晰,或者是我们对某些概念理解得不够透彻。比如,有人可能在使用某个工具或者库的时候,遇到了错误或者不明白怎么用。这种情况下,大家通常会去网上查找解决方案,比如在StackOverflow上提问或者寻找答案。

在这些讨论中,大家会分享自己的经验,提供一些代码示例,帮助彼此理解问题的根源和解决方法。通过这些交流,编程小白也能慢慢积累知识,提升自己的技能。

总之,编程的学习过程就是不断地提问、寻找答案和实践的过程。遇到问题不要怕,积极去寻求帮助,慢慢就能掌握更多的知识。

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()
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()
3
if shape in ("triangle", "Triangle")
if shape.lower() == "triangle"

或者更好的是,

撰写回答