点.py打印但停止打印

2024-06-23 03:02:50 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我差不多做完这件事了。所以我被困在一个双循环,它不打印后,打印(赢),所以有些事情是不正确的,但我不知道是什么问题,但这里的代码。而且它没有为一个玩家储存笔记、提醒或点数。如果有人能帮我,我将不胜感激。你知道吗

    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:

Tags: theinloopforlenallbutover
1条回答
网友
1楼 · 发布于 2024-06-23 03:02:50

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

如果您需要将all赢款与all玩家配对,请使用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

相关问题 更多 >

    热门问题