我一直尝试获得随机选择,直到满足if
块中的条件(即,当三个连续的选择返回相同结果时)。
面临的挑战是,我不知道如何将选择设置为继续直到满足条件,因为似乎必须指定k
。因此,k
的变量值不能由满足条件的时间决定。
#import random module
import random
# initiate x which would contain predictions
x = 0
#define prediction; which would continue until 3 consecutive predictions get same outcome
while True:
x = (random.choices(['H','T'],k = 10 ))
print(x)
if x[-3:] == ['H','H','H'] or x[-3:] == ['T','T','T']:
break