一、完善球賽程式,測驗球賽你程式、所有函式的測驗結果:
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):
return a==15 or b==15
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 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))
print("2019310143016 盧佳純")
def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
print("排球競技比賽賽制規定:")
print("1.前4局比賽采用25分制,每個隊只有贏得至少25分,并同時超過對方2分時,才勝1局;")
print("2.正式比賽采用5局3勝制,決勝局的比賽采用15分制,一隊先得8分后,兩隊交換場區,按原位置順序繼續比賽到結束;")
print("3.在決勝局(第五局)之比賽,先獲15分并領先對方2分為勝;")
main()

測驗函式:
首先對GameOver(a,b)函式進行測驗
再對simOneGame(proA,proB)函式進行測驗
后對simNGames(n,proA,proB)函式進行測驗
def GameOver(N,scoreA,scoreB):
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)
ai=[]
bi=[]
try:
for scoreA,scoreB in ((1,25),(1,26),(25,25),(16,17),(28,30)):
if GameOver(scoreA,scoreB):
ai.append(scoreA)
bi.append(scoreB)
except:
print('Error')
print(ai)
print(bi)
#對simOneGame(proA,proB)函式進行測驗
from random import random
try:
probA,probB=0.5,0.5
scoreA,scoreB=0,0
serving = "A"
if serving == "A":
if random() < probA:
scoreA += 1
else:
serving="B"
else:
if random() < probB:
scoreB += 1
else:
serving="A"
print(scoreA)
print(scoreB)
except:
print('Error')
#對simNGames(n, probA, probB)
try:
n,scoreA,scoreB=1,1,21
winsA, winsB = 0, 0
scoreA_ls=[]
scoreB_ls=[]
for i in range(n):
scoreA_ls.append(scoreA)
scoreB_ls.append(scoreB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
print(winsA, winsB)
print(scoreA_ls,scoreB_ls)
except:
print('Error')

二、用requests庫的get()函式訪問百度網站,列印回傳狀態,text()內容,計算text()和content屬性所回傳的頁面內容的長度:
import requests
def getHTMLText(url):
try:
for i in range(0,20):
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = 'utf-8'
return r.status_code,r.text,r.content,len(r.text),len(r.content)
except:
return ""
url = 'http://www.baidu.com.cn/'
print(getHTMLText(url))
結果為:

三、制作一個簡單的html頁面:a、獲取body標簽的內容;b、獲取id為first的標簽物件;c、獲取并列印html頁面中的中文字符
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<b>第一個html頁面————來自學號2019310143016</b><br><br>
<p id="first">制作者的基本資訊:</p>
</body>
<table border="1">
<tr>
<td>班級</td>
<td>姓名</td>
<td>年級</td>
</tr>
<tr>
<td>信計1班</td>
<td>純牛奶</td>
<td>19級</td>
</tr>
</table>
</html>

四、利用爬蟲獲取2017中國大學排名:
import requests
from bs4 import BeautifulSoup
import bs4
import pandas as pd
info = []#用來存放爬取資訊
url ="http://www.zuihaodaxue.com/Greater_China_ranking2017_0.html"
try:
r=requests.get(url,timeout=100)
r.raise_for_status()
r.encoding=r.apparent_encoding
soup = BeautifulSoup(r.text,"html.parser")
for tr in soup.find("tbody").children:
if isinstance(tr,bs4.element.Tag):
tds=tr.find_all("td")
info.append([tds[0].string,tds[1].string,tds[3].string])
print("{0:^10}\t{1:{3}^10}\t{2:^10}".format("排名","學校名稱","總分",chr(12288)))
for i in range(50):
print("{0:^10}\t{1:{3}^10}\t{2:^10}".format(info[i][0],info[i][1],info[i][2],chr(12288)))
name = ["排名","學校名稱","總分"]
test = pd.DataFrame(columns=name,data=https://www.cnblogs.com/cnn-ljc/p/info)
test.to_csv(r"C:\code_python\test1.csv")
print("保存成功")
except Exception as e :
print(e)


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/145725.html
標籤:Python
