2017-01-07 23 views
-2

私は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 
+0

ようこそ[so]。 [mcve]の作成方法を見直し、エラー出力を含め、問題の原因を教えてください。 – TemporalWolf

+1

あなたの質問はJavaと何が関係していますか? –

答えて

1

total = 0 
totalScore = total + score 

を変更することができますそれはループの外側です。

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 
0

まず、ループ外にtotalScoreを定義します。その後、あなたは、単に要素の合計の範囲がループ内にあるため、グローバル値が0取られるたびに完了するので、持ってしようとしないで、特に

total+=score 
関連する問題