问题

如果来自r1&r2的一个或多个坐标相同,那么(“矩形确实重叠”)

如果没有坐标相同,那么(“矩形不重叠”)

如果所有坐标都是相同的,那么(“矩形是相同的”)

所以,当所有坐标相同时,我的代码打印出"The rectangles do overlap.""The rectangles are the same." – 但我只是希望它打印"The rectangles are the same.".怎么能做到?

 shape = input("Select (rectangle / circle): ")


if shape == "rectangle":

x1 = float(input("Enter rectangle A's left coordinate: "))
x2 = float(input("Enter rectangle A's right coordinate: "))
y1 = float(input("Enter rectangle A's top coordinate: "))
y2 = float(input("Enter rectangle A's bottom coordinate: "))

x01 = float(input("Enter rectangle B's left coordinate: "))
x02 = float(input("Enter rectangle B's right coordinate: "))
y01 = float(input("Enter rectangle B's top coordinate: "))
y02 = float(input("Enter rectangle B's bottom coordinate: "))

r1 = (int(x1), int(x2), int(y1), int(y2))
r2 = (int(x01), int(x02), int(y01), int(y02))

if any(item in r1 for item in r2):
    print("The rectangles do overlap.")

if all(item in r1 for item in r2):
    print("The rectangles are the same.")

else:
    print("The rectangles do not overlap.")
 

  最佳答案

你的问题是,如果all(...)是真的,any(...)也是真的.只需更改if语句的顺序并使用elif:

 if all(item in r1 for item in r2):
    print("The rectangles are the same.")
elif any(item in r1 for item in r2):
    print("The rectangles do overlap.")
else:
    print("The rectangles do not overlap.")
 

  相同标签的其他问题

python-3.x