?前言
不知不覺今天已經過去大半,本以為昨天就能更新的文章今天的這個時候才開始碼字,今天總結兩個專案:猜數字和預測概率,編程可以讓人明白一些道理,甚至讓你覺得看似不相關的亂數其實也是有被預測的可能,那么接下來就來玩一玩這兩組代碼吧!
??猜數字
假設Joe的年齡是18歲,她被詢問年齡時不想直接做出直接的回答,但出于禮貌又有必要告訴對方,于是Joe想寫一個程式,通過給出提示讓別人猜測,這里就要用到while陳述句來構造這樣一個程式,
joe_age = 18
guess = int(input(">>:"))
while guess != joe_age:
if guess > joe_age:
print("猜的太大了,往小試試……")
elif guess < joe_age:
print("猜的太小了,往大試試……")
guess = int(input(">>:"))
print("恭喜你,猜對了!")
while回圈不同于for回圈就在于它可以無限回圈,我們不能知道要猜多少次,所以while回圈更加適合這樣的場景,
guess = int(input(">>:"))
同時也用到if和elif條件判斷陳述句:
if guess > joe_age:
print("猜的太大了,往小試試……")
elif guess < joe_age:
print("猜的太小了,往大試試……")
guess = int(input(">>:"))
print("恭喜你,猜對了!")
當猜測年齡大于實際年齡,列印“猜的太大了,往小試試……”,當猜測年齡小于實際年齡,列印“猜的太小了,往大試試……”,當猜測年齡等于實際年齡,列印"恭喜你,猜對了!"程式運行結果如下圖所示:

??獲勝概率預測
場景如下:
1.小A和小B羽毛球比賽,小A每球獲勝概率55%,小B每球獲勝概率45%;
2.每局比賽,先得21分者獲勝;
3.假設進行10000局比賽,兩人分別會獲勝多少局?
首先構造頂層框架:
def main():
#主要邏輯
prob_A, prob_B, number_of_games = get inputs() #獲取原始資料
win_A, win_B = sim_n_games(prob_A, prob_B, number_of_games) #獲取模擬結果
print_summary(win_A, win_B, number_of_games) #結果匯總輸出
def main()定義函式main, prob_A小A每球獲勝概率,prob_B小B每球獲勝概率,number_of_games比賽場數,通過以上引數,再獲得兩人分別贏多少局 win_A, win_B,最后進行匯總輸出print_summary,
錄入原始資料
def get_inputs():
#輸入原始資料
prob_A = eval(input("請輸入運動員A的每球獲勝概率(0~1):"))
prob_B = round(1-prob_A, 2) #2人比賽,1-prob_A = prob_B
number_of_games = eval(input("請輸入模擬比賽場數(正整數):"))
print("模擬比賽場數:", number_of_games)
print("小A每球獲勝概率:", prob_A)
print("小B每球獲勝概率:", prob_B)
return prob_A, prob_B, number_of_games #每一次return陳述句出現代表呼叫函式結束
prob_A, prob_B, number_of_games = get_inputs()
print(prob_A, prob_B, number_of_games)

多場比賽模擬
進行多場比賽模擬( score_A, score_B是獲得比分,其中一人到達 21回圈停止:
def sim_n_games(prob_A, prob_B, number_of_games): #定義sim_n_games函式
#模擬多場比賽的結果
win_A, win_B = 0, 0 #初始化A和B獲勝的場次
for i in range(number_of_games): #迭代number_of_games次,以下是模擬一場比賽,for回圈一萬次
score_A, score_B = sim_one_game(prob_A, prob_B) #獲得模擬比賽的比分
if score_A > score_B:
win_A +=1
else:
win_B +=1
return win_A, win_B
單次比賽情況模擬
然后我們再進行簡化,把多場比賽簡化成模擬一場比賽,再進行10000次回圈:
import random
def sim_one_game(prob_A, prob_B):
#模擬一場比賽的結果
score_A, score_B = 0,0
while not game_over(score_A, score_B): #這里用到while回圈
if random.random() < prob_A:
score_A +=1
else :
score_B +=1
return score_A, score_B
注意上面代碼中 if random.random() < prob_A: 隨機資料小于小A每球獲勝概率的55%,則A獲勝一次,此處需要用心理解,
匯總輸出
def print_summary(win_A, win_B, number_of_games):
print("共模擬了{}場比賽" .format(number_of_games)) #模擬總場次
print("選手A獲勝{0}場,占比{1:.1%}" .format(win_A, win_A/number_of_games)) #A獲勝的概率除以總場次乘以100%
print("選手B獲勝{0}場,占比{1:.1%}" .format(win_B, win_B/number_of_games)) #B獲勝的概率除以總場次乘以100%
最終形成程式
import random
def get_inputs():
#輸入原始資料
prob_A = eval(input("請輸入運動員A的每球獲勝概率(0~1):"))
prob_B = round(1-prob_A, 2) #2人比賽,1-prob_A = prob_B
number_of_games = eval(input("請輸入模擬比賽場數(正整數):"))
print("模擬比賽場數:", number_of_games)
print("小A每球獲勝概率:", prob_A)
print("小B每球獲勝概率:", prob_B)
return prob_A, prob_B, number_of_games #每一次return陳述句出現代表呼叫函式結束
#prob_A, prob_B, number_of_games = get_inputs()
#print(prob_A, prob_B, number_of_games)
def game_over(score_A, score_B):
#單場模擬結束條件是一方獲得21分
return score_A == 21 or score_B == 21
def sim_one_game(prob_A, prob_B):
#模擬一場比賽的結果
score_A, score_B = 0,0
while not game_over(score_A, score_B):
if random.random() < prob_A:
score_A += 1
else :
score_B += 1
return score_A, score_B
def sim_n_games(prob_A, prob_B, number_of_games): #定義sim_n_games函式
#模擬多場比賽的結果
win_A, win_B = 0, 0 #初始化A和B獲勝的場次
for i in range(number_of_games): #迭代number_of_games次,以下是模擬一場比賽,for回圈一萬次
score_A, score_B = sim_one_game(prob_A, prob_B) #獲得模擬比賽的比分
if score_A > score_B:
win_A += 1
else:
win_B += 1
return win_A, win_B
def print_summary(win_A, win_B, number_of_games):
print("共模擬了{}場比賽" .format(number_of_games)) #模擬總場次
print("選手A獲勝{0}場,占比{1:.1%}" .format(win_A, win_A/number_of_games)) #A獲勝的概率除以總場次乘以100%
print("選手B獲勝{0}場,占比{1:.1%}" .format(win_B, win_B/number_of_games)) #B獲勝的概率除以總場次乘以100%
def main():
#主要邏輯
prob_A, prob_B, number_of_games = get_inputs() #獲取原始資料
win_A, win_B = sim_n_games(prob_A, prob_B, number_of_games) #獲取模擬結果
print_summary(win_A, win_B, number_of_games) #結果匯總輸出
if __name__ == "__main__":
main()

到此,整個程式運行完畢,代碼中的注釋部分需要認真閱讀,
??小結
兩個小案例的練習,讓我覺得真的是每一個簡單的自動化執行背后都是無數的辛酸,社會大分工后讓人們的專業越來越細,越來越專,普羅大眾漫不經心地使用機器進行自動化高效作業,殊不知越是高效、便捷、傻瓜式的智能,說明背后越是有人付出時間和精力幫助你完成更多要考慮到的事情,極客萬歲!另外,概率分析這個案例,單個球的輸贏概率A和B相差無幾,但多次比賽下來卻出現驚人的差距,正所謂“失之毫厘,差之千里啊!”
下章再見!???♀?
未經溝通轉載,將追究法律責任,請尊重原創勞動成果!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/194781.html
標籤:Python
上一篇:Python3標準庫:textwrap文本自動換行與填充
下一篇:7.python虛擬環境詳解
