while循环打印1到10之间的随机数,当数字为N时停止

Nik*_*rud 3 python random while-loop

这就是我所拥有的,但这只生成一次随机数并无限地打印该数字:

import random

x = random.randint(0,10)
y = 7
while x != y:
    print(x)
Run Code Online (Sandbox Code Playgroud)

Jon*_*nts 6

像(在一段时间内移动条件):

stop_at = 7
while True:
    num = random.randint(0, 10)
    if num == stop_at:
        break
    print num
Run Code Online (Sandbox Code Playgroud)

或者,一个完整的重要因素:

from itertools import starmap, repeat, takewhile
from random import randint

for num in takewhile(lambda L: L != 7, starmap(randint, repeat( (0, 10) ))):
    print num
Run Code Online (Sandbox Code Playgroud)