Point.py打印但停止打印

时间:2014-01-08 10:17:38

标签: python

好的,所以我差不多完成了这件事。因此我陷入了双循环并且在打印后没有打印(奖金)所以有些东西不对,但我不确定是什么错,但这里是代码。它并没有为1名玩家存储音符或警报或积分。如果有人可以帮助我,我会很感激。

    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:

1 个答案:

答案 0 :(得分:1)

如果winnings是长度为1的列表,则for o in range(1, len(winnings)):循环将不会执行循环体,因为范围为空:

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

如果您不想跳过第一个元素,请不要在1处开始范围,而是从0循环:

>>> 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