这里以体育竞技模拟程序内涵数为例
原完整代码
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Wed May 15 11:38:02 2019 4 5 @author: lenovo 6 """ 7 8 import random 9 import math 10 def printIntro(): 11 print("这个程序模拟量个选手A和B的乒乓球比赛") 12 print("程序运行需要A和B的能力值(以0到1之间的小数表示)") 13 print("作者:呆。 (02)") 14 def getInputs(): 15 a = eval(input("请输入选手A的能力值(0-1): ")) 16 b = eval(input("请输入选手B的能力值(0-1): ")) 17 n = eval(input("模拟比赛的场次: ")) 18 return a, b, n 19 20 def printSummary(winsA, winsB): 21 n = winsA + winsB 22 print("竞技分析开始, 共模拟{}场比赛".format(n)) 23 print("选手A获胜{}场比赛, 占比{:0.1%}".format(winsA, winsA/n)) 24 print("选手B获胜{}场比赛, 占比{:0.1%}".format(winsB, winsB/n)) 25 26 def gameOver(a, b): 27 return (a==11 and b<10) or (b==11 and a<10) or (a>=10 and b>=10 and math.fabs(a-b)==2) 28 29 def simoneGame(probA, probB): 30 scoreA, scoreB = 0, 0 31 if random.random() < 0.5: 32 serving = "A" 33 else : 34 serving = "B" 35 while not gameOver(scoreA, scoreB): 36 if serving == "A": 37 if random.random() < probA: 38 scoreA += 1 39 else: 40 serving = "B" 41 else: 42 if random.random() < probB: 43 scoreB += 1 44 else: 45 serving = "A" 46 return scoreA, scoreB 47 def simOneGame(probA, probB): 48 winsA, winsB = 0, 0 49 for i in range(7): 50 scoreA, scoreB = simoneGame(probA, probB) 51 if scoreA > scoreB: 52 winsA += 1 53 else: 54 winsB += 1 55 return winsA, winsB 56 def simNGames(n ,probA, probB): 57 winsA, winsB = 0, 0 58 for i in range(n): 59 scoreA, scoreB = simOneGame(probA, probB) 60 if scoreA > scoreB: 61 winsA += 1 62 else: 63 winsB += 1 64 return winsA, winsB 65 66 def main(): 67 printIntro() 68 probA, probB, n = getInputs() 69 winsA, winsB = simNGames(n, probA, probB) 70 printSummary(winsA, winsB) 71 main()
分离函数单独测试
首先测试最底层函数 gameOver(a, b)
import math def gameOver(a, b): return (a==11 and b<10) or (b==11 and a<10) or (a>=10 and b>=10 and math.fabs(a-b)==2) print(gameOver(9,11)) print(gameOver(11,7)) print(gameOver(13,11)) print(gameOver(11,11)) print(gameOver(10,11))
结果正常:
由于gameOver函数无误,故借用该函数测试函数simoneGame(probA, probB)
import random import math def gameOver(a, b): return (a==11 and b<10) or (b==11 and a<10) or (a>=10 and b>=10 and math.fabs(a-b)==2) def simoneGame(probA, probB): scoreA, scoreB = 0, 0 if random.random() < 0.5: serving = "A" else : serving = "B" while not gameOver(scoreA, scoreB): if serving == "A": if random.random() < probA: scoreA += 1 else: serving = "B" else: if random.random() < probB: scoreB += 1 else: serving = "A" return scoreA, scoreB a,b=simoneGame(0.5, 0.5) print(a) print(b)
多次运行结果:
与期望相同
在借用上述函数测试函数simOneGame(probA, probB)
import random import math def gameOver(a, b): return (a==11 and b<10) or (b==11 and a<10) or (a>=10 and b>=10 and math.fabs(a-b)==2) def simoneGame(probA, probB): scoreA, scoreB = 0, 0 if random.random() < 0.5: serving = "A" else : serving = "B" while not gameOver(scoreA, scoreB): if serving == "A": if random.random() < probA: scoreA += 1 else: serving = "B" else: if random.random() < probB: scoreB += 1 else: serving = "A" return scoreA, scoreB def simOneGame(probA, probB): winsA, winsB = 0, 0 for i in range(7): scoreA, scoreB = simoneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsB a,b=simOneGame(0.5, 0.5) print(a) print(b)
结果如下:
竞技规则为7局4胜
故结果出错改动如下:
def simOneGame(probA, probB): winsA, winsB = 0, 0 for i in range(7): scoreA, scoreB = simoneGame(probA, probB) if winsA==4 or winsB==4: continue elif scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsB
改动后结果正确
同理嵌套检测函数simNGames(n ,probA, probB)及main函数
过程:略
原文地址:https://www.cnblogs.com/DXL123/p/10909245.html
时间: 2024-11-10 14:09:30