模块代码运行不正常

时间:2016-02-24 17:28:09

标签: python function python-3.x module

我正在尝试编写一些代码来显示用户选择的三面区域。如果输入不正确,我需要显示“输入无效”。当输入不正确时,我的代码不会正确计算区域或显示“输入无效”。我真的可以使用这个帮助。这是我需要做的事情的图片:

DateTimeFormatter.ISO_ORDINAL_DATE

这是我的代码:

side1, side2, side3 = eval(input("Enter three sides in a double: "))

def isValid(side1, side2, side3):

    if side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1: 
        return True
    else: 
        return None

def area(side1, side2, side3):

    s = (side1 + side2 + side3) / 2;

    totalArea = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5    

    return totalArea

def main():

    condition = isValid(side1, side2, side3)

    totalArea = area(side1, side2, side3)

    if True:
        print("The area of the triangle is " + str(totalArea))
    else:
        print("input is invalid")

main()

1 个答案:

答案 0 :(得分:1)

不要说if True因为它会永远发生。相反,请说if condition。此外,在isValid()中,您只需返回支票结果:

return side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1

您的完整代码应如下所示:

side1, side2, side3 = eval(input("Enter three sides in a double: "))

def isValid(side1, side2, side3):
    return side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1 

def area(side1, side2, side3):

    s = (side1 + side2 + side3) / 2;

    totalArea = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5    

    return totalArea

def main():

    condition = isValid(side1, side2, side3)

    totalArea = area(side1, side2, side3)

    if condition:
        print("The area of the triangle is " + str(totalArea))
    else:
        print("input is invalid")

main()