我试图一直获取随机选择,直到满足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
目前没有回答
相关问题 更多 >
编程相关推荐