def isValid(side1, side2, side3):
if (((side1 + side2) > side3)
and ((side1 + side3) > side2)
and ((side2 + side3) > side1)):
return True
else:
return False
def area(side1, side2, side3):
s = (side1 + side2 + side3) / 2
area = round(math.sqrt(s * (s - side1) * (s - side3) * (s - side2)), 2)
return area;
def main():
side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
side1 = eval(side1)
side2 = eval(side2)
side3 = eval(side3)
valid = True
valid = isValid(side1, side2, side3)
while True:
if valid is not True:
print("This is not a valid triangle.")
print()
print("Would you like to go again? ")
answer = input()
if answer == "yes" or "Yes":
return True
elif answer == "no" or "No":
return False
else:
return
break
else:
x = area(side1, side2, side3)
print("The area of the triangle is ", x)
main()
答案 0 :(得分:1)
def main():
while True:
side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
side1 = eval(side1)
side2 = eval(side2)
side3 = eval(side3)
valid = isValid(side1, side2, side3)
if not valid:
print("This is not a valid triangle.")
print()
while True:
print("Would you like to go again? (yes/no)")
answer = input()
if answer in ("no", "No"):
return
elif answer in ("yes", "Yes"):
break
else:
x = area(side1, side2, side3)
print("The area of the triangle is ", x)
return
答案 1 :(得分:0)
在您的代码中进行修改:
if answer == "yes" or answer=="Yes":
return True
elif answer == "no" or answer=="No":
return False
else:
return
我的方式:
减少输入
answer = input()
answer=answer.lower()
if answer == "yes" :
return True
elif answer == "no":
return False
else:
return
<强>修饰:强>
import math
def isValid(side1, side2, side3):
if (((side1 + side2) > side3) and ((side1 + side3) > side2) and ((side2 + side3) > side1)):
return True
else:
return False
def area(side1, side2, side3):
s = (side1 + side2 + side3) / 2
area = round(math.sqrt(s * (s - side1) * (s - side3) * (s - side2)), 2)
return area;
def main():
while True:
side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
side1 = int(side1)
side2 = int(side2)
side3 = int(side3)
valid = True
valid = isValid(side1, side2, side3)
if valid is not True:
print("This is not a valid triangle.")
print("\n")
print("Would you like to go again? ")
answer = input()
answer=answer.lower()
if answer == "yes" :
continue
elif answer == "no":
break
else:
break
else:
x = area(side1, side2, side3)
print("The area of the triangle is ", x)
main()
答案 2 :(得分:0)
您的代码无效。一旦你打电话给return True
,你的程序就会结束,如果用户说'是',你看起来好像要继续循环。
def area():
side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
side1 = eval(side1)
side2 = eval(side2)
side3 = eval(side3)
valid = isValid(side1, side2, side3)
if valid:
x = area(side1, side2, side3)
print("The area of the triangle is ", x)
return True
else:
print("This is not a valid triangle.\n")
print("Would you like to go again? ")
answer = input().lower()
if answer == 'no':
return False
else:
return area()
我们可以使迭代而不是递归,如果我们期望一个邪恶的克星通过每次给你的脚本一个无效的三角形而导致堆栈溢出。
def area():
side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
side1 = eval(side1)
side2 = eval(side2)
side3 = eval(side3)
valid = isValid(side1, side2, side3)
if valid:
x = area(side1, side2, side3)
print("The area of the triangle is ", x)
return True
else:
return False
while True:
success = area()
if success:
return True
else:
print("This is not a valid triangle.\n")
print("Would you like to go again? ")
answer = input().lower()
if answer == 'no':
return False
答案 3 :(得分:0)
我希望这段代码可以按要求运行。所做的更改是:
在else:block中插入return x,这样在打印之后,如果你想进一步使用该值,x也被分配给r。因此用r = main()
替换了main()def isValid(side1, side2, side3):
if (((side1 + side2) > side3) and ((side1 + side3) > side2) and ((side2 + side3) > side1)):
return True
else:
return False
def area(side1, side2, side3):
s = (side1 + side2 + side3) / 2
area = round(math.sqrt(s * (s - side1) * (s - side3) * (s - side2)), 2)
return area
def main():
flag = True
while flag:
side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ")
valid = isValid(side1, side2, side3)
if valid is not True:
print("This is not a valid triangle.")
print()
print("Would you like to go again? ")
answer = raw_input()
if answer.lower() == "yes":
flag = True
elif answer.lower() == "no":
flag = False
else:
x = area(side1, side2, side3)
print("The area of the triangle is ", x)
return x
r = main()