gpt4 book ai didi

python - 模块代码运行不正确

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:55 25 4
gpt4 key购买 nike

我正在尝试编写一些代码来显示用户选择的三个边的区域。如果输入不正确,我需要显示“输入无效”。当输入不正确时,我的代码将无法正确计算面积或显示“输入无效”。我真的需要帮助。这是我需要完成的工作的图片:

enter image description here

这是我的代码:

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

最佳答案

不要说if True,因为这样它总会发生。相反,说if条件。另外,在 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()

关于python - 模块代码运行不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com