Python 在while循环中从原始输入中断

Python 在while循环中从原始输入中断,python,input,while-loop,Python,Input,While Loop,我已经用Python编写了一个非常简单的掷骰子脚本。它允许你滚动三次。然而,我不知道如何打破while循环并在最后一次避免原始输入 #!/usr/bin/python from random import randrange, uniform def rollDice(): dice = randrange(3,18) print ("You rolled: %s" % dice) maxReRoll = 2 c = 0 reRoll = "y" while reRoll in

我已经用Python编写了一个非常简单的掷骰子脚本。它允许你滚动三次。然而,我不知道如何打破while循环并在最后一次避免原始输入

#!/usr/bin/python

from random import randrange, uniform

def rollDice():
  dice = randrange(3,18)
  print ("You rolled: %s" % dice)

maxReRoll = 2
c = 0
reRoll = "y"

while reRoll in ["Yes", "yes", "y", "Y"]:
  if c > maxReRoll:
    break
  else:
    rollDice()
    c+=1
    reRoll = raw_input("Roll again?  y/n ")

只需要一点交换

while reRoll in ["Yes", "yes", "y", "Y"]:
  rollDice()
  c+=1
  if c >= maxReRoll:  # notice the '>=' operator here
    break
  else:
    reRoll = raw_input("Roll again?  y/n ")

这应该适合您:

from random import randrange


def roll_dice():
    dice = randrange(3,18)
    print("You rolled: %s" % dice)

max_rolls = 2
c = 0
re_roll = "y"

while re_roll.lower() in ["yes", "y"] and (c < max_rolls):
    roll_dice()
    c += 1
    if c != max_rolls:
        re_roll = input("Roll again?  y/n ")
来自随机导入范围
def掷骰子():
骰子=随机范围(3,18)
打印(“您掷了%s”%骰子)
最大转鼓数=2
c=0
re_roll=“y”
在[“是”、“y”]和(c
你说你想让人们滚3次,但你有
maxReRoll=2