2014-01-08 72 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