【发布时间】:2018-05-01 22:45:55
【问题描述】:
这是我的代码:
import random
def coinFlip():
games = input("How many games? ")
tosses = input("How many coin tosses per game? ")
wins = [0, 0, 0]
for i in range(games):
gA = 0
gB = 0
prof = 0
for j in range(tosses):
flip1 = random.randint(0, 1)
flip2 = random.randint(0, 1)
if (flip1 == 0 and flip2 == 0):
gA += 1
elif (flip1 == 1 and flip2 == 1):
gB += 1
else:
prof += 1
gAper = ((gA * 1.0) / tosses) * 100
gBper = ((gB * 1.0) / tosses) * 100
profper = ((prof * 1.0) / tosses) * 100
print "Game {}:".format(i)
print " Group A: {} ({}%); Group B: {} ({}%); Prof: {}
({}%)".format(gA, gAper, gB, gBper, prof, profper)
if (gA > gB and gA > prof):
wins[0] += 1
elif (gB > gA and gB > prof):
wins[1] += 1
elif ( prof > gA and prof > gB):
wins[2] += 1
gAWper = ((wins[0] * 1.0) / games) * 100
gBWper = ((wins[1] * 1.0) / games) * 100
profWper = ((wins[2] * 1.0) / games) * 100
print "Wins: Group A = {} ({}%); Group B = {} ({}%); Prof: {}
({}%)".format(wins[0], gAWper, wins[1], gBWper, wins[2], profWper)
运行时,它会做它需要做的一切。它模拟掷两个硬币,跟踪每次抛硬币的赢家,每场比赛的赢家,并计算所有事情的百分比。唯一的问题是我无法完全弄清楚如何实现决胜局以从 A 组、B 组或教授中随机选择获胜者。该程序需要能够处理 2 路和 3 路平局。
【问题讨论】:
-
“我想不通”不构成 Stack Overflow 问题。你被困在哪里了?你想不出任何打破平局的方法吗?您不知道如何使现有代码适应您选择的方法吗?
标签: python python-2.7 random choice