您好 我是python的新手。我不知道为什么这段代码不起作用:
def area_finder_rectangle (height,width):
area = height*width
print("The area of your polygon is %.2f" % area)
def area_finder_triangle (height,width):
area = (height*width)*0.5
print("The area of your polygon is %.2f" % area)
while True:
print ("Choose the polygon that you want to calculate the area from.")
print ("Polygons supported: rectangle, triangle")
value = str(input("Your polygon is a: "))
print ("Be sure to include decimals even if you are going to input an integer")
if value == "rectangle" or "Rectangle":
heightt = float(input("Enter height: "))
widthh = float(input("Enter width: "))
area_finder_rectangle (height = heightt,width = widthh)
elif value == "triangle" or "Triangle":
heightt = float(input("Enter height: "))
widthh = float(input("Enter width: "))
area_finder_triangle (height = heightt,width = widthh)
print ("")
当您选择矩形时很好,但是当您选择三角形时,会发生这种情况:
>>> Choose the polygon that you want to calculate the area from. Polygons supported: rectangle, triangle Your polygon is a: triangle Be sure to include decimals even if you are going to input an integer Enter height: 10 Enter width: 10 The area of your polygon is 100.00
答案 0 :(得分:3)
if value == "rectangle" or "Rectangle":
不是你的想法。它会将value
与表达式"rectangle" or "Rectangle"
的完整结果进行比较,该结果将始终评估为True
。它应该是这样的:
if value == "rectangle" or value == "Rectangle":
但更好的选择是
if value.lower() == "rectangle":
甚至更好,直接在输入处转换为小写。
value = str(input("Your polygon is a: ")).lower()
旁注:
如果要将值与多个值进行比较,可以这样做:
if value in ["rectangle", "Rectangle", "RECTANGLE"]:
但在这种情况下,这不是一个最佳选择。在比较之前只需转换为小写。
这是完整的代码:
def area_finder_rectangle (height,width):
area = height*width
print("The area of your polygon is %.2f" % area)
def area_finder_triangle (height,width):
area = (height*width)*0.5
print("The area of your polygon is %.2f" % area)
while True:
print ("Choose the polygon that you want to calculate the area from.")
print ("Polygons supported: rectangle, triangle")
value = str(input("Your polygon is a: ")).lower()
print ("Be sure to include decimals even if you are going to input an integer")
if value == "rectangle":
heightt = float(input("Enter height: "))
widthh = float(input("Enter width: "))
area_finder_rectangle (height = heightt,width = widthh)
elif value == "triangle":
heightt = float(input("Enter height: "))
widthh = float(input("Enter width: "))
area_finder_triangle (height = heightt,width = widthh)
print ("")
一个测试人员:
[klutt@klutt-sandbox tmp]# python3 polygon.py
Choose the polygon that you want to calculate the area from.
Polygons supported: rectangle, triangle
Your polygon is a: rectangle
Be sure to include decimals even if you are going to input an integer
Enter height: 10.0
Enter width: 10.0
The area of your polygon is 100.00
Choose the polygon that you want to calculate the area from.
Polygons supported: rectangle, triangle
Your polygon is a: triangle
Be sure to include decimals even if you are going to input an integer
Enter height: 10.0
Enter width: 10.0
The area of your polygon is 50.00
您应该考虑的一件事是,在这种情况下,您可以更容易地发现问题,就是在无效输入的情况下始终添加else
。
if value == "rectangle":
<do something>
elif value == "triangle":
<do something>
else:
print("Unknown polygon")
答案 1 :(得分:3)
您的分支if value == "rectangle" or "Rectangle:"
将始终被拍摄,您将始终计算一个矩形。如果你放入括号,为什么这样做更有意义。
if (value == "rectangle") or ("Rectangle"):
如果value
是'triangle',则第一部分的计算结果为False,但or-condition的第二部分计算结果为True
,因为在Python中,布尔上下文中的非空字符串被视为True
要获得您想要的效果,您需要说
if (value == "rectangle") or (value == "Rectangle"):