zoukankan      html  css  js  c++  java
  • try-except函数使用

    本文为上一个比赛规则的函数测试

    利用try-except函数测试是否函数错误

     1 def gameOver(a,b):
     2     if (a>=25 and abs(a-b)>=2 )or(b>=25 and abs(a-b)>=2):
     3         return True
     4     else:
     5         return False
     6     ai=[]
     7     bi=[]
     8     try:
     9         for a,b in ((25,25),(26,25),(27,26),(29,25)):
    10             if gameOver(a,b):
    11                 ai.append(a)
    12                 bi.append(b)
    13     except:
    14         print('Error')
    15     print(ai)
    16     print(bi)

    这里是先对gameover函数判断测试

      出来的结果是无错误(比分都属于正常比分,其中一队大于等于25并且领先另一队2分获胜)

     1 try:
     2     probA,probB=0.5,0.5
     3     scoreA,scoreB=0,0
     4     serving = "A"
     5     if serving == "A":
     6         if random() < probA:
     7             scoreA += 1
     8         else:
     9             serving="B"
    10     else:
    11         if random() < probB:
    12             scoreB += 1
    13         else:
    14             serving="A"
    15     print(scoreA) 
    16     print(scoreB)
    17 except:
    18     print('Error')

    这里是对修改后的(因为懒得多次输入参数)simOneGame(proA,proB)函数进行测试

      发现也没有错误的对局比分出现,其结果为24,26(结果不唯一)

     1 try:
     2     n,scoreA,scoreB=1,24,26
     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')

    这里是对simNGames(n, probA, probB)进行测试,函数结果为队伍赢得局数

      运行后为0,1,[24],[26]

  • 相关阅读:
    TestNG入门教程-6-enabled和priority属性
    TestNG入门教程-5-timeOut属性
    TestNG入门教程-4-Testng中注释简介
    Unicode、UTF-8 和 ISO8859-1到底有什么区别
    java数目
    sql必知必会-总结篇
    mysql监控、性能调优及三范式理解
    loadrunner常用函数总结
    loadrunner监控度量项及中文解释
    linux下crontab的使用实现
  • 原文地址:https://www.cnblogs.com/qq1079179226/p/10909019.html
Copyright © 2011-2022 走看看