try except函数测试函数
Posted shuxincheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了try except函数测试函数相关的知识,希望对你有一定的参考价值。
对一个体育竞技模拟程序的各个函数进行测试
原程序代码如下
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Wed May 15 15:25:46 2019 4 5 @author: Benny 6 """ 7 8 #两个选手A,B的羽毛球竞技比赛预测 9 from random import random 10 def printIntro(): 11 print("这个程序模拟两个选手A和B的羽毛球竞技比赛") 12 print("程序运行需要A和B的能力值(以0到1之间的小数表示)") 13 def getInputs(): 14 a = eval(input("请输入选手A的能力值(0-1): ")) 15 b = eval(input("请输入选手B的能力值(0-1): ")) 16 n = eval(input("模拟比赛的场次: ")) 17 m = eval(input("模拟次数:")) 18 return a, b, n, m 19 def simNGames(n, probA, probB): 20 winsA, winsB = 0, 0 21 scoreA_ls=[] 22 scoreB_ls=[] 23 for i in range(n): 24 scoreA, scoreB = simOneGame(probA, probB) 25 scoreA_ls.append(scoreA) 26 scoreB_ls.append(scoreB) 27 if scoreA > scoreB: 28 winsA += 1 29 else: 30 winsB += 1 31 return winsA, winsB,scoreA_ls,scoreB_ls 32 def gameOver(a,b): 33 if(a>=20 or b>=20): 34 if(abs(a-b)==2 and a<=29 and b<=29): 35 return True 36 else: 37 return a==30 or b==30 38 else: 39 return False 40 def simOneGame(probA, probB): 41 scoreA, scoreB = 0, 0 42 serving = "A" 43 while not gameOver(scoreA, scoreB): 44 if serving == "A": 45 if random() < probA: 46 scoreA += 1 47 else: 48 serving="B" 49 else: 50 if random() < probB: 51 scoreB += 1 52 else: 53 serving="A" 54 return scoreA, scoreB 55 def printSummary(winsA, winsB,scoreA_ls,scoreB_ls): 56 n = winsA + winsB 57 print("竞技分析开始,共模拟{}场比赛".format(n)) 58 print("A选手各场次得分比分:") 59 print(scoreA_ls) 60 print("B选手各场次得分比分:") 61 print(scoreB_ls) 62 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) 63 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) 64 def main(): 65 printIntro() 66 probA, probB, n, m= getInputs() 67 for i in range(m): 68 winsA, winsB,scoreA_ls,scoreB_ls = simNGames(n, probA, probB) 69 printSummary(winsA, winsB,scoreA_ls,scoreB_ls) 70 n=input("按任何键退出") 71 72 main()
1、首先对函数gameOver(a,b)进行测试
1 def gameOver(a,b): 2 if(a>=20 or b>=20): 3 if(abs(a-b)==2 and a<=29 and b<=29): 4 return True 5 else: 6 return a==30 or b==30 7 else: 8 return False 9 if gameOver(1,21): 10 print(‘Ture‘) 11 else: 12 print(‘False‘)
输入几种特殊情况
1 def gameOver(a,b): 2 if(a>=20 or b>=20): 3 if(abs(a-b)==2 and a<=29 and b<=29): 4 return True 5 else: 6 return a==30 or b==30 7 else: 8 return False 9 ai=[] 10 bi=[] 11 try: 12 for a,b in ((1,20),(1,21),(20,20),(21,22),(23,30)): 13 if gameOver(a,b): 14 ai.append(a) 15 bi.append(b) 16 except: 17 print(‘Error‘) 18 19 print(ai) 20 print(bi)
函数能正常输出如下:
1 [23] 2 [30]
但是理想输出应该为
[1, 23]
[21, 30]
然后观察程序代码发现第2,3行应改为如下
1 if(a>20 or b>20): 2 if(abs(a-b)>=2 and a<=29 and b<=29):
更改后得到预期结果
2、现在对simOneGame(proA,proB)函数进行测试(改写部分代码方便测试)
1 from random import random 2 try: 3 probA,probB=0.5,0.5 4 scoreA,scoreB=0,0 5 serving = "A" 6 if serving == "A": 7 if random() < probA: 8 scoreA += 1 9 else: 10 serving="B" 11 else: 12 if random() < probB: 13 scoreB += 1 14 else: 15 serving="A" 16 print(scoreA) 17 print(scoreB) 18 except: 19 print(‘Error‘) 20
程序运行成功而且结果无误:结果为1 0
3、最后对simNGames(n, probA, probB)进行测试(改写部分代码方便测试)
1 try: 2 n,scoreA,scoreB=1,1,21 3 winsA, winsB = 0, 0 4 scoreA_ls=[] 5 scoreB_ls=[] 6 for i in range(n): 7 scoreA_ls.append(scoreA) 8 scoreB_ls.append(scoreB) 9 if scoreA > scoreB: 10 winsA += 1 11 else: 12 winsB += 1 13 print(winsA, winsB,scoreA_ls,scoreB_ls) 14 except: 15 print(‘Error‘)
程序运行正常,结果为0 1 [1] [21]
以上是关于try except函数测试函数的主要内容,如果未能解决你的问题,请参考以下文章
使用java.lang.Exception的错误:测试类应该只有一个公共构造函数