You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么Python 2.7自定义图形判断函数满足条件时仍打印false

代码问题说明
  • isPara 函数参数使用错误:定义的形参是s1、s2,判断逻辑中错误使用了未声明的变量b1、b2,基础判断逻辑本身无法正常运行。
  • isPara 函数无返回值:仅在函数内部打印判断结果,未将结果返回给调用方,外部逻辑无法拿到平行四边形的判断结论。
  • isRec 函数调用逻辑错误:if isPara is True是判断函数对象本身是否等于布尔值True,该条件永远不成立,因此始终走else分支输出不符合预期的结果。
  • 语法兼容性问题:使用了Python2专属的无括号print写法,在Python3环境下会直接报错。
修正后的完整代码
# 平行四边形判断函数
def isPara(s1, s2):
    '''对边长度相等则返回True'''
    if s1 == s2:
        res = True
        print("True")
    else:
        res = False
        print("False")
    return res

# 矩形判断函数
def isRec(s1, s2, angle):
    '''满足平行四边形条件且夹角为90度则返回True'''
    if isPara(s1, s2) and angle == 90:
        res = True
        print("True")
    else:
        res = False
        print("Not true")
    return res

s1 = 3 
s2 = 3
angle = 90

isPara(s1, s2)
isRec(s1, s2, angle)
运行结果
True
True
True

内容的提问来源于stack exchange,提问作者Dean OKeefe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:06:05