Skip to main content
 首页 » 编程设计

python之值错误 :invalid for int() with base 10: ''

2025年05月04日10duanxz

我正在编写一个 Python 程序,它接受一个字符串,它是一个双边方程,“(equation) = (equation)”,并通过涂黑可以在任一侧的两个字符来求解两侧,以便方程解彼此相等

到目前为止,我有这段代码,但对于任何不是单边方程式的情况,我都会不断收到错误消息。以下是完整的回溯:

    Traceback (most recent call last): 
  File "C:/Users/rgade_000/Desktop/blackout.py", line 69, in <module> 
    main() 
  File "C:/Users/rgade_000/Desktop/blackout.py", line 65, in main 
    print("+11x4 =",compute("+11x4"), "(expect None)") 
  File "C:/Users/rgade_000/Desktop/blackout.py", line 22, in compute 
    temp_num_int = int(temp_num_str) 
ValueError: invalid literal for int() with base 10: '' 

我知道这是 op == "" 语句的问题,但我不知道如何解决它。该问题仅出现在我尝试使用的任何字符串方程式中,例如在主调用中

print("solving 288/24x6=18x13x8: ", solve("288/24x6=18x13x8")) 

(主函数包含测试用例)

def compute(exp): 
    """ 
    Takes a string representing an equation and computes the value 
    """ 
    temp_num_str = "" 
    temp_num_int = 0 
    op = "" 
    for ch in exp: 
        if ch.isdigit(): 
            if temp_num_str == "": 
                temp_num_str = ch 
            else: 
                temp_num_str += ch 
        else: 
            if op == "": 
                op = ch 
                temp_num_int = int(temp_num_str) 
            else: 
                temp_num_int = eqs(temp_num_int, op, temp_num_str) 
                op = ch 
            temp_num_str = "" 
    return eqs(temp_num_int, op, temp_num_str) 
 
def eqs(temp_num_int, op, temp_num_str): 
    if op == "+": 
        return temp_num_int + int(temp_num_str) 
    elif op == "-": 
        return temp_num_int - int(temp_num_str) 
    elif op == "x": 
        return temp_num_int * int(temp_num_str) 
    else: 
        return temp_num_int / int(temp_num_str) 
 
def solve(eqn): 
    """ 
    Takes two sided equation and blacks out values so that the equations are equal 
    """ 
    for i in range(len(eqn)): 
        for j in range(i+1,len(eqn)): 
            if i != eqn.find('=') and j != eqn.find('='): 
                eqn_tmp = eqn[:i] + eqn[i+1:j]+eqn[j+1:] 
                if evaluate(eqn_tmp) == True: 
                    return eqn_tmp 
                else: 
                    print("No solution") 
 
def evaluate(eqn): 
    """ 
    Evaluates a string of two sided equation and returns True if equal 
    """ 
    lt = eqn[:eqn.find('=')] 
    rt = eqn[eqn.find('=')+1:] 
    if (compute(lt) == compute(rt)): 
        return True 
    else: 
        return False 
 
def main(): 
    print("22-11x4 =", compute("22-11x4"), " (expect 44)") 
    print("+11x4 =",compute("+11x4"), "(expect None)") 
    print("22-11x4=7x5+9", evaluate("22-11x4=7x5+9"),"(expect True)") 
    print("solving 288/24x6=18x13x8: ", solve("288/24x6=18x13x8")) 
 
main() 

请您参考如下方法:

错误消息告诉您 temp_num_str 在您尝试将其转换为整数时不包含任何内容。

    temp_num_int = int(temp_num_str) 
ValueError: invalid literal for int() with base 10: '' 

解决此问题的方法是在您认为正在为 temp_num_str 赋值的每个地方都放置打印语句,并找出它没有获得您期望的值的地方,然后解决由此发现的所有问题。

(实际上,阅读代码,它至少清楚了一个可能出错的方式:你第一次进入循环

for ch in exp: 
    if ch.isdigit(): 

ch 可能不是数字。这导致

    else: 
        if op == "": 
            op = ch 
            temp_num_int = int(temp_num_str) 

在 temp_num_str 有一个值之前被执行

    if ch.isdigit(): 
        if temp_num_str == "": 
            temp_num_str = ch 
        else: 
            temp_num_str += ch 

)