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

Python三角形面积计算程序异常处理后执行报错排查

三角形面积计算函数的TypeError问题解答

问题背景

需要实现带异常处理的三角形面积计算函数,具体要求:

  • 实现checkTriangleEdges函数,验证边长大于0且能构成三角形,不符合则抛出对应ValueError;
  • 实现requestTriangleEdges函数,获取用户输入并调用checkTriangleEdges验证,异常时重复获取直到输入合法,返回边长元组;
  • 实现calculateTriangleArea函数,调用requestTriangleEdges获取边长并计算面积。

编写的代码如下:

import math

def checkTriangleEdges(a, b, c):
    if a<=0 or b<=0 or c<=0:
        raise ValueError("All triangle edges must be > 0")
    if a+b<c or b+c<a or c+a<b:
        raise ValueError("a, b and c can not form a triangle")

def requestTriangleEdges():
    try:
        a = float(input("Length of edge a: "))
        b = float(input("Length of edge b: "))
        c = float(input("Length of edge c: "))
        checkTriangleEdges(a, b, c)
        return (a, b, c)
    except ValueError as e:
        print(e)
        requestTriangleEdges()

def calculateTriangleArea():
    a, b, c = requestTriangleEdges()
    p = (a + b + c) / 2
    s = p * (p - a) * (p - b) * (p - c)
    result = math.sqrt(s)
    return print(result)

calculateTriangleArea()

错误现象

直接输入合法数据时程序正常运行,但输入错误数据(如非数字、无效边长)后,再输入正确值,程序抛出TypeError: cannot unpack non-iterable NoneType object错误,报错信息如下:

Length of edge a: x
could not convert string to float: 'x'
Length of edge a: 3
Length of edge b: 10
Length of edge c: 1
a, b and c can not form a triangle
Length of edge a: 0
Length of edge b: 1
Length of edge c: 3
All triangle edges must be > 0
Length of edge a: 4
Length of edge b: 4
Length of edge c: 4
Traceback (most recent call last):
  File "C:\Users\Alex\Documents\HTML5\Fundamentals-S1\tp7.py", line 42, in <module>
    calculateTriangleArea()
  File "C:\Users\Alex\Documents\HTML5\Fundamentals-S1\tp7.py", line 32, in calculateTriangleArea
    a, b, c = requestTriangleEdges()
TypeError: cannot unpack non-iterable NoneType object

错误原因

问题出在requestTriangleEdges函数的异常处理分支:当捕获到ValueError后,调用requestTriangleEdges()进行递归,但没有返回这个递归调用的结果。这意味着第一次调用requestTriangleEdges遇到错误时,执行完except块的代码后,函数默认返回None。后续即使递归分支成功返回了边长元组,最外层的函数调用仍然返回None,导致calculateTriangleArea里的a, b, c = requestTriangleEdges()试图解包None,触发TypeError。

修复方案

在except块里递归调用requestTriangleEdges()时,加上return,把递归的返回值传递出去;同时简化calculateTriangleArea里的打印逻辑:

import math

def checkTriangleEdges(a, b, c):
    if a<=0 or b<=0 or c<=0:
        raise ValueError("All triangle edges must be > 0")
    if a+b<c or b+c<a or c+a<b:
        raise ValueError("a, b and c can not form a triangle")

def requestTriangleEdges():
    try:
        a = float(input("Length of edge a: "))
        b = float(input("Length of edge b: "))
        c = float(input("Length of edge c: "))
        checkTriangleEdges(a, b, c)
        return (a, b, c)
    except ValueError as e:
        print(e)
        return requestTriangleEdges()  # 添加return传递递归返回值

def calculateTriangleArea():
    a, b, c = requestTriangleEdges()
    p = (a + b + c) / 2
    s = p * (p - a) * (p - b) * (p - c)
    result = math.sqrt(s)
    print(result)  # 直接打印,无需return print

calculateTriangleArea()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 05:35:28