python - if-else,在 python 上使用 True 和 False 语句。雕塑网

标签 python if-statement

这是我必须创建它的代码,因此它允许

  1. 用户输入三角形三边的长度为 Length1、Length2 和 Length3
  2. 如果任意两条边的长度相同,程序输出“Isosceles”
  3. 否则程序输出“不是等腰”

但是,输出部分似乎不起作用。 我是这种编码的新手,所以请帮忙?

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/

相关文章:

javascript - 从 javascript 调用本地 python 脚本

c# - 如果在第一个 `else` 之后有一个 `return` ,这对性能有影响吗?

php - 在 PHP 中检查两个变量是否不为空

Haskell 在一种条件下定义多个变量?

c++ - 如果是数字,则将字符串转换为 int

python - 无法分配 "<class ' django.contrib.auth.models.User' >": "Model.user"必须是 "User"实例

python - 无法安装pandas库

python - QPixmap保持纵横比

python - 尝试在同一设备上录制和播放时 PyAudio 无法工作

C - 将负值变为正值并在函数外部使用它