【发布时间】:2020-04-15 01:12:57
【问题描述】:
我有以下代码,我试图验证用户提供的输入是否大于零,但返回的值不是正确的值 - 例如,如果我第一次输入零角度,然后输入大于零的值,返回的是零而不是正确的值。关于如何解决的任何想法?
def ValidTriangle(firstAngle, secondAngle, thirdAngle):
if (firstAngle + secondAngle + thirdAngle) == 180:
print("The triangle is indeed a valid triangle")
else:
print("The triangle is not valid")
def ValidateInput(angle):
if angle <=0:
isValid = False
else:
isValid = True
while isValid == False:
try:
angle = int(input('Please try again'))
break
except ValueError:
return(angle)
angle1 = int(input("Please enter the first angle of the triangle: \n"))
ValidateInput(angle1)
angle2 = int(input("Please enter the second angle of the triangle: \n"))
ValidateInput(angle2)
angle3 = int(input("Please enter the third angle of the triangle: \n"))
ValidateInput(angle3)
ValidTriangle(angle1,angle2,angle3)
【问题讨论】:
-
ValidateInput仅在出现错误时返回;没关系,因为您不会对返回的任何值做任何事情。 -
变量和函数名应该遵循
lower_case_with_underscores风格。
标签: python validation input