NameError:未定义名称

时间:2016-08-09 16:57:22

标签: python

我试图分析一些数据,我的代码如下:

echo 'document.write("... value=\""+arr_selections_json+"\" />");'; 

我不断收到一条错误消息:

for line in h:

    if line_cnt in start_x:
        recording_scores = True
        temp_i = start_x.index(line_cnt)
        score_acc = [0, 0, 0]
        codon_id = remainder_x[temp_i]
        temp_z = line.split()
        temp_score = float(temp_z[1])  
        score_acc[codon_id] += temp_score
        codon_id = (codon_id + 1) % 3 

    if temp_i>0 and line_cnt == end_x[temp_i]:
        score_x0[temp_i] = score_acc[0] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
        score_x1[temp_i] = score_acc[1] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
        score_x2[temp_i] = score_acc[2] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
        temp_i = -1
        recording_scores = False 

我以为我在第一个if语句中定义了temp_i,但是定义是不是继承到第二个if语句?有人能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

正如摩根指出的那样,如果你没有进入第一个循环,你就不会定义temp_iline_cnt。我不知道你想要做什么,但如果你只想测试第二个条件,如果第一个条件成立,你可能就是这样做的。

for line in h:

    if line_cnt in start_x:
        recording_scores = True
        temp_i = start_x.index(line_cnt)
        score_acc = [0, 0, 0]
        codon_id = remainder_x[temp_i]
        temp_z = line.split()
        temp_score = float(temp_z[1])  
        score_acc[codon_id] += temp_score
        codon_id = (codon_id + 1) % 3 

        if temp_i>0 and line_cnt == end_x[temp_i]:
            score_x0[temp_i] = score_acc[0] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
            score_x1[temp_i] = score_acc[1] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
            score_x2[temp_i] = score_acc[2] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
            temp_i = -1
            recording_scores = False 

否则,您可能希望在else语句中定义temp_iline_cnt:     for the line in h:

    if line_cnt in start_x:
        recording_scores = True
        temp_i = start_x.index(line_cnt)
        score_acc = [0, 0, 0]
        codon_id = remainder_x[temp_i]
        temp_z = line.split()
        temp_score = float(temp_z[1])  
        score_acc[codon_id] += temp_score
        codon_id = (codon_id + 1) % 3 
    else:
        temp_i = 0
        line_cnt = foo

    if temp_i>0 and line_cnt == end_x[temp_i]:
        score_x0[temp_i] = score_acc[0] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
        score_x1[temp_i] = score_acc[1] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
        score_x2[temp_i] = score_acc[2] / ((end_x[temp_i] - start_x[temp_i] + 1) / 3)
        temp_i = -1
        recording_scores = False