2014-01-08 112 views
0

好吧,所以我差不多完成了這件事。所以我被困在一個雙循環中,打印後沒有打印(贏錢),所以有些東西不對,但我不確定有什麼問題,但是這裏是代碼。而且它沒有爲單個玩家存儲筆記或提醒或積分。如果任何人都可以幫助我,我將不勝感激。Point.py打印,但停止打印

winnings = [] 
    for n in range(len(ratios)): 
     winnings.append(pot*ratios[n]) 
    print(winnings) #STOPS HERE 
    for winning in winnings[1:]: 
     # loop over all but the first element in winnings 
     winning = int(winning) 
     for i, player in enumerate(players[1:]): 
     # loop over all but the first player, adding indices 
     notes.store("~lottery~", player, "The system has placed you %s in the lottery. The lottery awarded you %s P$" % (Point.ordinal(i), winning), time.time()) 
     alerts.append(player) 
     point = Point.dPoint[player] + winning 
     Point.dPoint[player] = point 
    return True 
    elif len(players) == 0: 
+0

「贏」是長度爲1的列表,也許? –

+0

那麼當我測試它時,獎金列表中只有1件東西,所以是的。 – user3103923

回答

1

如果winnings是一個長度爲1的列表,然後for o in range(1, len(winnings)):循環將不執行循環體的範圍爲空:

>>> list(range(1, 1)) 
[] 

如果你不是故意要跳過第一要素,不從0,而不是啓動範圍內1,循環:

>>> range(0, 1) 
[0] 

Python的指數是從0開始的。

請注意,在Python中,您通常在列表上直接循環,而不是生成索引,然後是索引。即使你仍然需要循環索引以及,你會使用enumerate()功能,爲您添加索引以循環:

winnings = [pot * ratio for ratio in ratios] 
for winning in winnings[1:]: 
    # loop over all but the first element in winnings 
    winning = int(winning) 
    for i, player in enumerate(players[1:]): 
     # loop over all but the first player, adding indices 
     notes.store("~lottery~", player, 
      "The system has placed you {} in the lottery. The lottery awarded " 
      "you {} P$".format(Point.ordinal(i), winning), time.time()) 
     alerts.append(player) 
     Point.dPoint[player] += winning 

如果您需要配對所有獎金與所有玩家,使用zip()

winnings = [pot * ratio for ratio in ratios] 
for i, (winning, player) in enumerate(zip(winnings, players)): 
    winning = int(winning) 
    notes.store("~lottery~", player, 
     "The system has placed you {} in the lottery. The lottery awarded " 
     "you {} P$".format(Point.ordinal(i), winning), time.time()) 
    alerts.append(player) 
    Point.dPoint[player] += winning 
+0

嗯,如果它是一個或多個列表,我將如何使它工作? – user3103923

+0

@ user3103923:那麼,在那種情況下,你希望發生什麼?爲什麼你循環索引1到最後而不是0到最後? –

+0

對於我需要從1開始而不是0開始的循環的玩家部分。 – user3103923