乒乓球
import _pyinstaller_hooks_contrib as pyinstaller
#a.采用乒乓球比赛规则
#1.打印程序的介绍性信息式(要求包括你们学号后两位信息)
print("学号后两位:40")
print("22信计1班曾翠")
from random import random
def printIntro():
print("这个程序模拟A和B两方的乒乓球竞技比赛")
print("程序运行需要A和B两方的能力值(以0到1之间的小数表示)")
#2.输入I(input):两个球员(球队)的能力值,模拟比赛的次数
def getInputs():
bs = eval(input("单打请输入1,双打请输入2:"))
a = eval(input("请输入A方的能力值(0-1):"))#此时因为有双打和单打,所以称之为A方和B方
b = eval(input("请输入B方的能力值(0-1):"))
n = eval(input("模拟比赛的场次:"))
return bs, a, b, n
#3.处理P(process):模拟比赛过程
#乒乓球比赛规则
#一局比赛:在一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。
#一场比赛:单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。
def simNGames(bs, N, probA, probB):
winsA, winsB = 0, 0
if bs == 1: # 单打
for i in range(N):
scoreA, scoreB = simOneGame(probA, probB, 7, 4)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
elif bs == 2: # 双打
for i in range(N):
scoreA, scoreB = simOneGame(probA, probB, 5, 3)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def simOneGame(probA, probB, num_sets, num_wins):
player1_score, player2_score = 0, 0
while not gameOver(player1_score, player2_score, num_wins):
point_winner = simulatePointWinner(probA, probB)
if point_winner == "A":
player1_score += 1
else:
player2_score += 1
return player1_score, player2_score
def simulatePointWinner(probA, probB):
if random() < probA:
return "A"
else:
return "B"
def gameOver(player1_score, player2_score, num_wins):
return player1_score == num_wins or player2_score == num_wins
#4.输出O(output):两个球员(球队)获胜的概率(输出谁做的结果截图)
def printSummary(winsA,winsB,n):
n = winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("A方获胜{}场比赛,获胜概率{:0.1%}".format(winsA, winsA / n))
print("B方获胜{}场比赛,获胜概率{:0.1%}".format(winsB, winsB / n))
def main():
printIntro()
bs, probA, probB, n = getInputs()
winsA, winsB = simNGames(bs, n, probA, probB)
printSummary(winsA, winsB, n)
main()
pyinstaller
第一次结果:
第二次结果:
第三次结果:
第四次结果: