球赛预测 篮球
import random def printIntro(): print("这个程序模拟两支球队A和B的篮球比赛") print("程序运行需要球队A和B的能力值(以0到1之间的小数表示)") print("作者:Regan_White_Lin 12") def getInputs(): a = eval(input("请输入球队A的能力值(0-1): ")) b = eval(input("请输入球队B的能力值(0-1): ")) n = random.randint(1,1000) return a, b, n def simOneGame(n,probA, probB): scoreA, scoreB = 0, 0 serving = "A" a=0 while (a<=n) or (a>n) and (scoreA==scoreB): if serving == "A": if random.random() < probA: scoreA += 1 else: serving="B" else: if random.random() < probB: scoreB += 1 else: serving="A" a+=1 return scoreA, scoreB def printSummary(n, scoreA, scoreB): print("竞技分析开始,共模拟{}个回合".format(n)) print("球队A获得{}分".format(scoreA)) print("球队B获得{}分".format(scoreB)) if scoreA>scoreB: char='A' else: char='B' print("获胜方是球队{}".format(char)) def main(): printIntro() probA, probB, n = getInputs() scoreA, scoreB = simOneGame(n,probA, probB) printSummary(n, scoreA, scoreB) main()