import random
from math import *
def printIntro():#打印程序的介绍性信息
print("模拟乒乓球竞赛")
print("学号31")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
try:
printIntro()
except:
print (‘Error‘)
def getInputs():#获得用户输入的参数
a = eval(input("请输入选手A的能力值(0-1): "))
b = eval(input("请输入选手B的能力值(0-1): "))
h= eval(input("请输入一场要打几局:"))
n = eval(input("模拟比赛的场次: "))
return a, b, h,n
try:
probA, probB, h,n = getInputs()
except:
print (‘Error‘)
def gameOver(scoreA, scoreB):
g=scoreA-scoreB
if (abs(g)==2 and scoreA> 10 and scoreB> 10) or (g> 0 and scoreA==11) or (g<0 and scoreB==11):
return scoreA,scoreB
try:
gameOver(11,11)
except:
print (‘Error‘)
def simOneGame(probA, probB,h):#模拟一场比赛
for i in range(h): #模拟七局四胜或五局三胜为一场
serving = "A"
roundA =roundB=0 #分别为队伍A和B的赢得的比赛的局次
scoreA, scoreB = 0, 0
while not gameOver(scoreA, scoreB): #模拟一局比赛
roundA=roundB=0
if serving == "A":
if random.random() < probA:
scoreA += 1
else:
serving = "B"
else:
if random.random() < probB:
scoreB += 1
else:
serving = "A"
if scoreA>scoreB:
roundA += 1
else:
roundB += 1
return roundA,roundB
try:
simOneGame(0.2,0.3,7)
except:
print (‘Error‘)
def simNGames(n ,probA, probB,h):#利用A,B的的能力值模拟N场比赛
winsA, winsB = 0, 0
for i in range(n):
roundA , roundB = simOneGame(probA, probB,h)
if roundA >roundB:
winsA += 1
else:
winsB += 1
return winsA, winsB
try:
simNGames(5,0.2,0.3,7)
except:
print (‘Error‘)
def printSummary(winsA, winsB):
n = winsA + winsB
print("竞技分析开始, 共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛, 占比{:0.1%}".format(winsA, winsA/n))
print("选手B获胜{}场比赛, 占比{:0.1%}".format(winsB, winsB/n))
try:
winsA, winsB = simNGames
from requests import *
try:
for i in range(20):
print("第",i+1,"次访问")
r=get("http://www.google.cn")
r.raise_for_status()
r.encoding=‘utf-8‘
print("网络状态码:",r.status_code)
print(r.text)
print("text属性长度:",len(r.text))
print("content属性长度:",len(r.content))
except:
print("Error")
原文:https://www.cnblogs.com/gzzfh/p/12883605.html