我在Python中编写了一个非常简单的骰子滚动脚本。它会让你滚三次。但是,我不知道如何打破while循环并在最后一次避免raw_input。
#!/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 ")
答案 0 :(得分:2)
只需要一点点交换。
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 ")
答案 1 :(得分:0)
这应该适合你:
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 ")