这是我必须创建它的代码,因此它允许
- 用户输入三角形三边的长度为 Length1、Length2 和 Length3
- 如果任意两条边的长度相同,程序输出“Isosceles”
- 否则程序输出“不是等腰”
但是,输出部分似乎不起作用。 我是这种编码的新手,所以请帮忙?
Length1 = raw_input()
Length2 = raw_input()
Length3 = raw_input()
print Length1
print Length2
print Length3
print Length1 == Length2
print Length2 == Length3
print Length1 == Length3
if Length1 == Length2 is True:
print "Isosceles"
else:
print "Not Isosceles"
if Length2 == Length3 is True:
print "Isosceles"
else:
print "Not Isosceles"
if Length1 == Length3 is True:
print "Isosceles"
else:
print "Not Isosceles"
最佳答案
问题是Python解释
if Length1 == Length2 is True:
喜欢
if Length1 == Length2 and Length2 is True:
Comparison operators, like <
, ==
, or is
are chained.这非常有用,例如a < b < c
,但它也可能导致一些意想不到的行为,就像您的情况一样。
将这些检查更改为
if (Length1 == Length2) is True:
或者更好,只需使用
if Length1 == Length2:
或者,您可以使用 set
计算不同 边的数量:
distinct_sides = len(set([Length1, Length2, Length3]))
if distinct_sides == 1:
print "Equilateral"
if distinct_sides == 2:
print "Isosceles"
if distinct_sides == 3:
print "Scalene"
关于python - if-else,在 python 上使用 True 和 False 语句。雕塑网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25314003/