为什么即使指定相同的“ if,else”语句后,输出也只要求“矩形的输入”而不是“三角形的输入”?

时间:2018-07-13 16:16:48

标签: python-3.x

def rectangle(b,c):
    area_rectangle = b * c
    print (area_rectangle)


def triangle(base,height):
    area_triangle = 1/2 * (base * height)
    print (area_triangle)


a = str(input("Area of what: Triangle or Rectangle? =>"))


if a == "rectangle" or "Rectangle" or "Rec" or "rec":
    rectangle((int(input("Please enter base of rectangle:"))),(int(input("Please enter height of rectangle:"))))
elif a == "Triangle" or "triangle":
    triangle((int(input("Please enter base of triangle:"))),(int(input("Please enter height of rectangle:"))))
else:
    print ("invalid Request")

1 个答案:

答案 0 :(得分:0)

使用此:

if a in ["rectangle" ,"Rectangle" ,"Rec" ,"rec"]:

与三角形相同。

发生这种情况是因为在python中

if a == 'x' or 'b' or 'c' or 'd' 

不同
if (a =='x') or (a =='b') or  (a == 'c') or ( a == 'd')