当前位置: 代码迷 >> python >> 输出中的Python错误
  详细解决方案

输出中的Python错误

热度:41   发布时间:2023-06-13 16:57:49.0

我写了下面的代码

t_p=0
f_p=0
f_n=0
t_n=0

while True:
    line=raw_input()
    if not line:
        break
    actual,predicted=line.split(',')
    print actual, predicted
    if (actual==1 and predicted==1):
        t_p+=50
        print "tp", t_p
    elif(actual==0 and predicted==1):
        f_p+=-25.1
        print "fp", f_p
    elif(actual==1 and predicted==0):
        f_n+=-50.0
        print "fn", f_n
    else:
        t_n=0.0
        print "tn", t_n

score=t_p+f_p+f_n+t_n
print score

现在,当我通过以下内容时:

0,0
0 0
tn 0.0
0,1
0 1
tn 0.0
1,0
1 0
tn 0.0
1,1
1 1
tn 0.0
1,1
1 1
tn 0.0

似乎总是总是取tn值,因为这两个变量的值满足其他条件,所以该值不应该是其他值。

line=raw_input()actual,predicted=line.split(',')将为您提供string actualpredicted ,该值永远不会等于int 1 类型的转换将解决此问题。

无需将输入数据转换为int ,就可以像字符串一样将它们保留下来,并将它们与验证数据进行比较,这些数据是类似于以下示例的tuples

t_p = 0
f_p = 0
f_n = 0
t_n = 0

while True:
    data = tuple(raw_input().split(','))

    if data[0] == 'q' or data[0] == '' :
        break
    else:
        print data


    if data == ('1','1'):
        t_p += 50 
        print 'tp', t_p

    if data == ('0','1'):
         f_p -= 25.1
         print 'fp', f_p

    if data == ('1', '0'):
        f_n -= 50.0
        print 'fn', f_n

    else:
        tn = 0.0
        print 'tn', t_n

score = t_n + f_n, + f_p, t_p
print score

演示:

1,1
('1', '1')
tp 50
tn 0
1,0
('1', '0')
fn -50.0
0,1
('0', '1')
fp -25.1
tn 0
0,0
('0', '0')
tn 0

(-50.0, -25.1, 50)
  相关解决方案