Discuss / Python / 作业

作业

Topic source

分析问题所得,发现在自定义函数str2num定义中的系统函数int位置,系统函数int的参数为数字或能表示数字的其他形式,故需要做出优化:

def str2num(n):
    try:
        n = int(n)
    except ValueError as e:
        try:
            n = float(n)
        except ValueError as e:
            raise ValueError(r"'%s' is not literal" % n)
    return
def str2num(n):
    try:
        n = int(n)
    except ValueError as e:
        try:
            n = float(n)
        except ValueError as e:
            raise ValueError(r"'%s' not literal" % n)
    return n

  • 1

Reply