python中的无限循环

python中的无限循环,python,while-loop,infinite-loop,Python,While Loop,Infinite Loop,我应该为以下问题编写代码: 玩家掷两个六面骰子。如果两个骰子的总和不是 第七,总和加在球员的总分和球员的 又开始了。当玩家掷骰总数为7时,游戏结束 结束 这就是我到目前为止所做的: def main(): dice1 = randrange(1, 7,) dice2 = randrange(1, 7,) roll = dice1 + dice2 score = 0 count = 0 while roll != 7: count = count + 1

我应该为以下问题编写代码:

玩家掷两个六面骰子。如果两个骰子的总和不是 第七,总和加在球员的总分和球员的 又开始了。当玩家掷骰总数为7时,游戏结束 结束

这就是我到目前为止所做的:

def main():
  dice1 = randrange(1, 7,)
  dice2 = randrange(1, 7,)

  roll = dice1 + dice2
  score = 0
  count = 0
  while roll != 7:
    count = count + 1
    score = score + roll
    if roll == 7:
     break
  print count
  print score

main()
然而,它只是给了我一个无限循环,当它应该滚动模具,直到模具的总和为7


如何修复它?

您需要在每次迭代中更新
roll
的值。现在,
roll
永远不会改变,所以它永远不会等于7(这意味着循环将永远运行)

我想你希望你的代码是这样的:

from random import randrange
def main():

  # Note that the following two lines can actually be made one by doing:
  # score = count = 0
  score = 0
  count = 0

  # There is really no need to define dice1 and dice2
  roll = randrange(1, 7,) + randrange(1, 7,)

  # This takes care of exiting the loop when roll = 7
  # There is no need for that if block
  while roll != 7:

    # Note that this is the same as count = count + 1
    count += 1

    # And this is the same as score = score + roll
    score += roll

    # Update the value of roll
    # This is actually the line that fixes your problem
    roll = randrange(1, 7,) + randrange(1, 7,)

  print count
  print score

main()

另外,请注意,我对脚本做了一些更改以提高效率。

您需要在每次迭代中更新
roll
的值。现在,
roll
永远不会改变,所以它永远不会等于7(这意味着循环将永远运行)

我想你希望你的代码是这样的:

from random import randrange
def main():

  # Note that the following two lines can actually be made one by doing:
  # score = count = 0
  score = 0
  count = 0

  # There is really no need to define dice1 and dice2
  roll = randrange(1, 7,) + randrange(1, 7,)

  # This takes care of exiting the loop when roll = 7
  # There is no need for that if block
  while roll != 7:

    # Note that this is the same as count = count + 1
    count += 1

    # And this is the same as score = score + roll
    score += roll

    # Update the value of roll
    # This is actually the line that fixes your problem
    roll = randrange(1, 7,) + randrange(1, 7,)

  print count
  print score

main()

另外,请注意,我对脚本做了一些更改以提高效率。

您必须在
循环中获得新的骰子编号

def main():
      dice1 = randrange(1, 7,)
      dice2 = randrange(1, 7,)

      roll = dice1 + dice2
      score = 0
      count = 0
      while roll != 7:
        count = count + 1
        score = score + roll
        if roll == 7:
          break
        dice1 = randrange(1, 7,)
        dice2 = randrange(1, 7,)
        roll = dice1 + dice2
      print count    
      print score

main()

您必须在
循环中获得新的骰子编号

def main():
      dice1 = randrange(1, 7,)
      dice2 = randrange(1, 7,)

      roll = dice1 + dice2
      score = 0
      count = 0
      while roll != 7:
        count = count + 1
        score = score + roll
        if roll == 7:
          break
        dice1 = randrange(1, 7,)
        dice2 = randrange(1, 7,)
        roll = dice1 + dice2
      print count    
      print score

main()

您没有更新
roll
;确保在循环中再次滚动:

roll = randrange(1, 7) + randrange(1, 7)
score = count = 0

while roll != 7:
    score += roll
    count += 1
    # re-roll the dice for the next round
    roll = randrange(1, 7) + randrange(1, 7)

一旦您将
dice1+dice2
分配给
roll
,它不会在每次循环后自行“重新滚动”骰子。

您没有更新
roll
;确保在循环中再次滚动:

roll = randrange(1, 7) + randrange(1, 7)
score = count = 0

while roll != 7:
    score += roll
    count += 1
    # re-roll the dice for the next round
    roll = randrange(1, 7) + randrange(1, 7)

一旦您将
dice1+dice2
分配给
roll
,它本身不会在每次循环后“重新滚动”骰子。

以下是解决您问题的优雅方法:

from random import randint

count, score = 0, 0
while True:
    roll = randint(1,6) + randint(1,6)
    if roll == 7:
        break
    count += 1
    score += roll

print count, score

以下是解决您的问题的优雅解决方案:

from random import randint

count, score = 0, 0
while True:
    roll = randint(1,6) + randint(1,6)
    if roll == 7:
        break
    count += 1
    score += roll

print count, score

您没有在while循环中更改roll的值。难怪它会以无限循环结束。同样,如果roll==7:break,您不需要
在roll!=7:
打印((lambda f:f(f,0,0,0,u导入u('random')))(lambda f,r,c,s,i:(c,s)如果r==7其他f(f,i.randint(1,6)+i.randint(1,6),c+1,s+r,i))
您不会更改while循环中的滚动值。难怪它会以无限循环结束。同样,如果roll==7:break,您不需要
在roll!=7:
print((lambda f:f(f,0,0,0,uu导入uu('random')))(lambda f,r,c,s,s,i:(c,s)如果r==7,则f(f,i.randint(1,6)+i.randint(1,6),c+1,s+r,i))
我会在顶部设置
roll=0
,以保持在一个位置(循环结束时)滚动的逻辑。这是一个额外的循环,但在性能方面并不昂贵。当然,但是您还必须将
count
设置为
-1
。我会将
roll=0
设置在顶部,以保持在一个位置(在循环的末尾)滚动的逻辑。这是一个额外的循环,但在性能方面并不昂贵。当然,但是您还必须将
count
设置为
-1