python概率計算器實例分析
更新時間:2015年03月25日 10:25:26 作者:令狐不聰
這篇文章主要介紹了python概率計算器實現(xiàn)方法,實例分析了Python實現(xiàn)概率計算的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了python概率計算器實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
from random import randrange
#randrange form random module
def calc_prob(strengths):
"""A function that receives an array of two numbers
indicating the strength of each party
and returns the winner"""
if strengths[1]>strengths[0]:
#Bring the bigger number to the first position in the array
temp=strengths[0]
strengths[0]=strengths[1]
strengths[1]=temp
prob1=abs(strengths[0]-strengths[1])
#The relative strength of the 2 parties
prob2=randrange(0,100)
#To calculate the luck that decides the outcome
if prob2 in range(0,33-prob1):
#Check if the weaker party is capable of winning.
#The condition gets narrower with the increase
#in relative strengths of each parties
return strengths[1]
elif prob2 in range(33-prob1,66-prob1):
#The middle condition
return "Draw"
else:
return strengths[0]
#Luck favors the stronger party and if relative strength
#between the teams is too large,
#the match ends up in favor of the stronger party
#Example
calc_prob([50,75]);#Always has to be a list to allow exchange
#Can be programmed in hundreds of better ways. Good luck!
希望本文所述對大家的Python程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
Keras:Unet網(wǎng)絡(luò)實現(xiàn)多類語義分割方式
本文主要利用U-Net網(wǎng)絡(luò)結(jié)構(gòu)實現(xiàn)了多類的語義分割,并展示了部分測試效果,希望對你有用!2020-06-06
Python3使用requests模塊實現(xiàn)顯示下載進度的方法詳解
這篇文章主要介紹了Python3使用requests模塊實現(xiàn)顯示下載進度的方法,結(jié)合實例形式分析了Python3中requests模塊的配置、使用及顯示進度條類的相關(guān)定義方法,需要的朋友可以參考下2019-02-02
Django中使用 Closure Table 儲存無限分級數(shù)據(jù)
對于數(shù)據(jù)量大的情況(比如用戶之間有邀請鏈,有點三級分銷的意思),就要用到 closure table 的結(jié)構(gòu)來進行存儲。這篇文章主要介紹了Django中使用 Closure Table 儲存無限分級數(shù)據(jù),需要的朋友可以參考下2019-06-06
基于python的Tkinter實現(xiàn)一個簡易計算器
這篇文章主要介紹了基于python的Tkinter實現(xiàn)一個簡易計算器的相關(guān)資料,還為大家分享了僅用用50行Python代碼實現(xiàn)的簡易計算器,感興趣的小伙伴們可以參考一下2015-12-12
Tortoise-orm信號實現(xiàn)及使用場景源碼詳解
這篇文章主要為大家介紹了Tortoise-orm信號實現(xiàn)及使用場景源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Python安全獲取域管理員權(quán)限幾種方式操作示例
在不考慮直接攻擊域控的情況下,如何快速獲取域管理員權(quán)限呢?本文分享幾種常見的獲取域管理員權(quán)限的方式,有需要的朋友可以借鑒參考下2021-10-10

