yshda

导航

 

对球赛程序函数进行测试

代码

from random import random

def printIntro():
    try:
        print("46号")
        print("这个程序模拟两个球队A和B的排球竞技比赛")
        print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
        print("{:*^70}".format("模拟开始"))
    except:
        return "Error"
    
def getInputs():
    try:    
        a=eval(input("请输入球队A的能力值(0-1):"))
        b=eval(input("请输入球队B的能力值(0-1):"))
        n=eval(input("模拟比赛的场次:"))
        return a,b,n
    except:
        return "Error"

def printSummary(winsA,winsB):
    try:
        n=winsA+winsB
        print("{:*^70}".format("模拟结束"))
        print("竞技分析开始,共模拟{}场比赛".format(n))
        print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
        print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
    except:
        return "Error"

def GameOver(N,scoreA,scoreB):
    try:
        if N <= 4:
            return (scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2)
        else:
            return (scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)
    except:
        return "Error"

def simAGame(N,probA,probB):
    try:
        scoreA,scoreB=0,0
        serving="A"
        while not GameOver(N,scoreA,scoreB):
            if serving=="A":
                if random() > probA:
                    scoreB+=1
                    serving="B"
                else:
                    scoreA+=1
            else:
                if random() > probB:
                    scoreA+=1
                    serving="A"
                else:
                    scoreB+=1
        return scoreA,scoreB
    except:
        return "Error"
     
def simOneGame(probA,probB):
    try:    
        winA,winB=0,0
        for N in range(5):
            scoreA,scoreB=simAGame(N,probA,probB)
            if scoreA > scoreB:
                winA+=1
            else:
                winB+=1
            if winA==3 or winB==3:
                break
        return winA,winB
    except:
        return "Error"
        

def simNGames(n,probA,probB):
    try:    
        winsA,winsB=0,0
        for i in range(n):
            winA,winB=simOneGame(probA,probB)
            if winA > winB:
                winsA+=1
            else:
                winsB+=1
        return winsA,winsB
    except:
        return "Error"
    
def main():
    try:
        printIntro()
        probA,probB,n=getInputs()
        winsA,winsB=simNGames(n,probA,probB)
        printSummary(winsA,winsB)
    except:
        return "Error"

main()

结果:

46号
这个程序模拟两个球队A和B的排球竞技比赛
程序运行需要A和B的能力值(以0到1之间的小数表示)
*********************************模拟开始*********************************

请输入球队A的能力值(0-1): 0.75
请输入球队B的能力值(0-1): 0.745
模拟比赛的场次: 10

*********************************模拟结束*********************************
竞技分析开始,共模拟10场比赛
选手A获胜8场比赛,占比80.0%
选手B获胜2场比赛,占比20.0%

 

Requests库

 

方法  说明
requests.request()  构造一个请求,支撑一下各方法的基础方法
requests.get()  获取HTML网页的主要方法,对应于HTTP的GET
requests.head() 获取HTML网页头信息的方法,对应于HTTP的HEAD
requests.post() 向HTML网页提交POST请求的方法,对应于HTTP的POST
requests.put() 向HTML网页提交PUT请求的方法,对应于HTTP的PUT
requests.patch()  向HTML网页提交局部修改请求,对应于HTTP的PATCH
requests.delete() 向HTML页面提交删除请求,对应于HTTP的DELETE
其对象属性 说明
r.states_code 获取返回的状态码
r.text / r.read() HTTP响应内容文本形式返回
r.content HTTP响应内容的二进制形式
r.json() HTTP响应内容的json形式
r.raw HTTP响应内容的原始形式
r.encoding   返回请求的url
r.encoding 从HTTP header中猜测的响应内容编码方式
r.apparent_encoding 从内容中分析出的响应内容编码方式(备选编码方式)
r.apparent_encoding 从内容中分析出的响应内容编码方式(备选编码方式)

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

具体代码:

import requests
url="https://www.google.cn/"
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding='utf-8'
for i in range(20):
    print(r.status_code,end=" ")
print("\n")
print(r.text)
print(len(r.content))
print(len(r.text))

结果

200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 

<!DOCTYPE html>
<html lang="zh">
  <meta charset="utf-8">
  <title>Google</title>
  <style>
    html { background: #fff; margin: 0 1em; }
    body { font: .8125em/1.5 arial, sans-serif; text-align: center; }
    h1 { font-size: 1.5em; font-weight: normal; margin: 1em 0 0; }
    p#footer { color: #767676; font-size: .77em; }
    p#footer a { background: url(//www.google.cn/intl/zh-CN_cn/images/cn_icp.gif) top right no-repeat; padding: 5px 20px 5px 0; }
    ul { margin: 2em; padding: 0; }
    li { display: inline; padding: 0 2em; }
    div { -moz-border-radius: 20px; -webkit-border-radius: 20px; border: 1px solid #ccc; border-radius: 20px; margin: 2em auto 1em; max-width: 650px; min-width: 544px; }
    div:hover, div:hover * { cursor: pointer; }
    div:hover { border-color: #999; }
    div p { margin: .5em 0 1.5em; }
    img { border: 0; }
  </style>
  <div>
    <a href="http://www.google.com.hk/webhp?hl=zh-CN&amp;sourceid=cnhp">
      <img src="//www.google.cn/landing/cnexp/google-search.png" alt="Google" width="586" height="257">
    </a>
    <h1><a href="http://www.google.com.hk/webhp?hl=zh-CN&amp;sourceid=cnhp"><strong id="target">google.com.hk</strong></a></h1>
    <p>请收藏我们的网址
  </div>
  <ul>
    <li><a href="http://translate.google.cn/?sourceid=cnhp">翻译</a>
  </ul>
  <p id="footer">&copy;2011 - <a href="http://www.miibeian.gov.cn/">ICP证合字B2-20070004号</a>
  <script nonce="woEJ3biqbDoNDdTKzKHrUw">
    var gcn=gcn||{};gcn.IS_IMAGES=(/images\.google\.cn/.exec(window.location)||window.location.hash=='#images'||window.location.hash=='images');gcn.HOMEPAGE_DEST='http://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp';gcn.IMAGES_DEST='http://images.google.com.hk/imghp?'+'hl=zh-CN&sourceid=cnhp';gcn.DEST_URL=gcn.IS_IMAGES?gcn.IMAGES_DEST:gcn.HOMEPAGE_DEST;gcn.READABLE_HOMEPAGE_URL='google.com.hk';gcn.READABLE_IMAGES_URL='images.google.com.hk';gcn.redirectIfLocationHasQueryParams=function(){if(window.location.search&&/google\.cn/.exec(window.location)&&!/webhp/.exec(window.location)){window.location=String(window.location).replace('google.cn','google.com.hk')}}();gcn.replaceHrefsWithImagesUrl=function(){if(gcn.IS_IMAGES){var a=document.getElementsByTagName('a');for(var i=0,len=a.length;i<len;i++){if(a[i].href==gcn.HOMEPAGE_DEST){a[i].href=gcn.IMAGES_DEST}}}}();gcn.listen=function(a,e,b){if(a.addEventListener){a.addEventListener(e,b,false)}else if(a.attachEvent){var r=a.attachEvent('on'+e,b);return r}};gcn.stopDefaultAndProp=function(e){if(e&&e.preventDefault){e.preventDefault()}else if(window.event&&window.event.returnValue){window.eventReturnValue=false;return false}if(e&&e.stopPropagation){e.stopPropagation()}else if(window.event&&window.event.cancelBubble){window.event.cancelBubble=true;return false}};gcn.resetChildElements=function(a){var b=a.childNodes;for(var i=0,len=b.length;i<len;i++){gcn.listen(b[i],'click',gcn.stopDefaultAndProp)}};gcn.redirect=function(){window.location=gcn.DEST_URL};gcn.setInnerHtmlInEl=function(a){if(gcn.IS_IMAGES){var b=document.getElementById(a);if(b){b.innerHTML=b.innerHTML.replace(gcn.READABLE_HOMEPAGE_URL,gcn.READABLE_IMAGES_URL)}}};
    gcn.listen(document, 'click', gcn.redirect);
    gcn.setInnerHtmlInEl('target');
  </script>

3244
3216

 

posted on 2020-05-13 16:02  yshda  阅读(223)  评论(0)    收藏  举报