如何使函数重启取决于答案(Python 2.7)

时间:2018-07-06 07:31:28

标签: python python-2.7

from random import randint
def roll_the_dice():
    dice = randint(1, 6)
    dice2 = randint(1, 6)
    print dice , dice2
    x = raw_input('If you want to reroll press 1 if not press 2:\n')
    if x == int(1):
        continue
    elif x == int(2):
        break
    else:
        print 'Invalid input'

roll_the_dice()

问题是我将continue放在哪里,如何使它重新启动取决于答案

1 个答案:

答案 0 :(得分:0)

如果您希望它运行到特定情况,可以将其放在 while true 循环中。当使用break and continue时,它处于循环上下文中。可能是 for 循环或任何类型的循环,但是如果您希望循环永久运行(或者直到您中断它),可以在true时使用

赞:

import re
s = "if the string is really2, I want to replace it"
re.sub(r'(\w+)2', r'\1-\1', s)
# 'if the string is really-really, I want to replace it'