zoukankan      html  css  js  c++  java
  • 第一个爬虫和测试

    完善球赛程序,测试所写程序,所有函数测试结果

    代码:

    from random import random
    def printIntro():
    print("模拟两支排球队A和B的排球比赛")
    print("A和B的能力值(以0到1之间的小数表示)")
    def getInputs():
    a=eval(input("请输入队伍A的能力值(0~1):"))
    b=eval(input("请输入队伍B的能力值(0~1):"))
    n=eval(input("模拟比赛的场次:"))
    return a,b,n
    def simNGames(n,probA,probB):
    winsA,winsB=0,0
    for i in range(n):
    scoreA,scoreB=simOneGame(probA,probB)
    if scoreA>scoreB:
    winsA +=1
    else:
    winsB +=1
    return winsA,winsB
    def gameOver(a,b):
    if (a>25 and abs(a-b)>=2 )or(b>25 and abs(a-b)>=2):
    return True
    else:
    return False
    def simOneGame(probA,probB):
    scoreA,scoreB=0,0
    serving = "A"
    while not gameOver(scoreA,scoreB):
    if serving =="A":
    if random()<probA:
    scoreA +=1
    else:
    serving="B"
    else:
    if random()<probB:
    scoreB +=1
    else:
    serving="A"
    return scoreA,scoreB
    def final(probA,probB):
    winsA,winsB=simNGames1(4,probA,probB)
    printSummary(winsA,winsB)
    if not winsA==3 or winsB==3:
    if winsA==winsB==2:
    winsA1,winsB1=simOneGame1(probA,probB)
    finalprintSummary(winsA,winsB)
    else:
    finalprintSummary(winsA,winsB)
    def simNGames1(n,probA,probB):
    winsA,winsB=0,0
    for i in range(n):
    scoreA,scoreB=simOneGame2(probA,probB)
    if winsA==3 or winsB==3:
    break
    if scoreA>scoreB:
    winsA+=1
    else:
    winsB+=1
    return winsA,winsB
    def simOneGame2(probA,probB):
    scoreA,scoreB=0,0
    serving="A"
    while not GG(scoreA,scoreB):
    if serving=="A":
    if random() < probA:
    scoreA += 1
    else:
    serving="B"
    else:
    if random() < probB:
    scoreB += 1
    else:
    serving="A"
    return scoreA,scoreB
    def simOneGame1(probA,probB):
    scoreA,scoreB=0,0
    serving="A"
    while not finalGameOver(scoreA,scoreB):
    if serving=="A":
    if random() < probA:
    scoreA += 1
    else:
    serving="B"
    else:
    if random() < probB:
    scoreB += 1
    else:
    serving="A"
    return scoreA,scoreB
    def GG(a,b):
    return a==3 or b==3
    def finalGameOver(a,b):
    if (a==8 or b==8):
    if a>b:
    print("A队获得8分,双方交换场地")
    else:
    print("B队获得8分,双方交换场地")
    if (a>15 and abs(a-b)>=2 )or(b>15 and abs(a-b)>=2):
    return True
    else:
    return False
    def finalprintSummary(winsA,winsB):
    n=winsA+winsB
    if n>=4:
    print("进行最终决赛")
    if winsA>winsB:
    print("最终决赛由A获胜")
    else:
    print("最终决赛由B获胜")
    else:
    if winsA>winsB:
    print("最终决赛由A获胜")
    else:
    print("最终决赛由B获胜")
    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))
    def main():
    printIntro()
    probA,probB,n=getInputs()
    winsA,winsB=simNGames(n,probA,probB)
    printSummary(winsA,winsB)
    final(probA,probB)
    try:
    main()
    except:
    print("Error")

    结果:

    用ruquests的get函数访问谷歌网站20次,打印返回状态,text()内容,计算text()属性和content属性所返回网页内容的长度

    代码:

    import requests
    for i in range(1,21):
    r = requests.get("https://www.google.cn/")
    r.raise_for_status()
    r.encoding='utf-8'
    print(r.status_code)
    print(r.text)
    len1=len(r.text)
    len2=len(r.content)
    print('text()属性所返回的长度{}'.format(len1))
    print('content()属性所返回的长度{}'.format(len2))

    结果:

  • 相关阅读:
    selinux 设置的彻底理解 并要 熟练经常的使用
    关于linux下自定义的 alias文件和自定义函数库的通用写法(只适合自己的)
    linux下关于mysql的命令的用法
    彻底地/ 终于地, 解决 关于apache 权限的问题了:: 修改 DocumentRoot后的 403错误: have no permission to access / on this server
    php的内核组成模块和运行原理
    彻底了解 suid, sgid ,sticky权限
    php编程疑难解决-1
    再次安装fedora23的一些遗留问题的解决
    word如何替换行首?
    php高级开发参考地址
  • 原文地址:https://www.cnblogs.com/wjxk/p/12882949.html
Copyright © 2011-2022 走看看