我需要帮助制作一个程序来判断你输入的形状是否是平行四边形

时间:2017-11-30 14:50:22

标签: python

我试图使a和b相同,当a和b相同时,c和d必须相同,但它们可以与a和b不同

ConfigureContainerBuilder()

2 个答案:

答案 0 :(得分:0)

您可以将您的逻辑简化为以下内容,而不是使用字符串文字'a', 'b'等,而是使用实际的变量名称:

a, b, c, d = sorted(
    float(input("Enter length of side {}: ".format(x))) for x in 'ABCD'
)  
if a == b and c == d:
    # parallel

答案 1 :(得分:0)

您需要将变量与变量进行比较,而不是字符串:

play = True

while play:

    a = float(input("Enter length of side A: "))
    b = float(input("Enter length of side B: "))
    c = float(input("Enter length of side C: "))
    d = float(input("Enter length of side D: "))

    if a == b and c == d:
        print("It's a parallelogram")

    elif a == c and b == d:
        print("It's a parallelogram")
    elif b == c and a == d:
        print("It's a parallelogram")
    else:
        print("It is not a parallelogram")

您可以进一步简化测试,并在函数中提取i / o:

def get_values():
    a = float(input("Enter length of side A: "))
    b = float(input("Enter length of side B: "))
    c = float(input("Enter length of side C: "))
    d = float(input("Enter length of side D: "))

    return a, b, c, d

play = True
while play:

    a, b, c, d = get_values()
    if (a == b and c == d) or (a == c and b == d) or (b == c and a == d):
        print("It's a parallelogram")
    else:
        print("It is not a parallelogram")