为什么输出只要求"Rectangle's input"而不要求"Triangle's input",即使在为相同的语句指定了"if, else"语句之后?


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")

使用此:

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')

最新更新