Python for循环将分数添加到总分

时间:2017-01-07 01:50:41

标签: java python image for-loop

我习惯使用Java但不熟悉Python,我试图让以下blob查找代码运行3次,增加每个分数并打印总分。

在Java中我会使用:

int totalScore = 0
for(int i=0; i<=3; i++)
    {
      blob code..;
      totalScore += score;
      i++
    }
System.out.println(totalScore);

如何将评分代码包装在执行3次的循环中,然后打印总分?任何帮助深表感谢。

from SimpleCV import *

def getscoreforrgb(rgb):
    return rgbmap[rgb] 

for score in range(3):


    blobsY = img1.findBlobs()
    if blobsY is not None:
        blobsY.sortArea()
        blobsY[-1].draw(Color.RED, width=3)
        y = blobsY[-1]
        print y


    blobsX = img1.findBlobs()
    if blobsX is not None:
        blobsX.sortArea()
        blobsX[-1].draw(Color.RED, width=3)
        x = blobsX[-1]
        print x

    colrgb =  pixcol[x2, y2]
    print colrgb
    score = getscoreforrgb(colrgb)

    total = 0
    totalScore = total + score

print totalScore

2 个答案:

答案 0 :(得分:1)

特别是,因为元素total的范围在循环内部并不是全局完成的,所以每次取值为0时,请尝试将其置于循环之外。

from SimpleCV import *
def getscoreforrgb(rgb):
    return rgbmap[rgb] 
total = 0
totalScore = 0
for score in range(3):


    blobsY = img1.findBlobs()
    if blobsY is not None:
        blobsY.sortArea()
        blobsY[-1].draw(Color.RED, width=3)
        y = blobsY[-1]
        print y


    blobsX = img1.findBlobs()
    if blobsX is not None:
        blobsX.sortArea()
        blobsX[-1].draw(Color.RED, width=3)
        x = blobsX[-1]
        print x

    colrgb =  pixcol[x2, y2]
    print colrgb
    score = getscoreforrgb(colrgb)

    totalScore = total + score

print totalScore

答案 1 :(得分:0)

首先,在循环外定义var payload = {id: user.id}; var token = jwt.sign(payload, jwtOptions.secretOrKey); 。然后,您只需更改

即可
totalScore

total = 0
totalScore = total + score