我是 python 的新手,我正在尝试创建一个 coinflip 循环,它将继续翻转并计算翻转次数,直到正面数量 = 反面数量,它将停止并打印它所花费的翻转总数达到这一点。我正在尝试获得结果以完成我的数学课程,但我似乎无法弄清楚如何让它停止或打印结果,当我这样做时它会打印 0。这是我的代码远的:
import random
heads = 1
tails = sum(random.choice(['head', 'tail']) == 'tail'
count = 0
while True:
coinresult = random.randint(1, 2) if heads == tails:
break
print("The number of flips was {count}".format(count = heads + tails))