掷两枚硬币的决胜局?

2024-10-04 07:36:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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路的关系。你知道吗


Tags: andformatgroup硬币randomgamesprintga
2条回答

不是直接给出答案,我只是告诉你如何解决这个问题。 你可以把你所有的获胜百分比都列在一张单子上。然后,通过列表理解和枚举,得到最大百分比的指标。一旦你有了索引,从中随机选择一个并宣布获胜。你知道吗

听起来,这只是一个问题,挑选一个随机的名字,从名单上的所有球员有一个单一的条件来处理时,它只是一个双向平局。不知道你要找的是不是这个。你知道吗

import random
player_list = ['gA', 'gB', 'prof']
random.choice(player_list)

相关问题 更多 >