python概率計(jì)算器實(shí)例分析
更新時(shí)間:2015年03月25日 10:25:26 作者:令狐不聰
這篇文章主要介紹了python概率計(jì)算器實(shí)現(xiàn)方法,實(shí)例分析了Python實(shí)現(xiàn)概率計(jì)算的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了python概率計(jì)算器實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(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!
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- Python實(shí)現(xiàn)扣除個(gè)人稅后的工資計(jì)算器示例
- 僅用50行代碼實(shí)現(xiàn)一個(gè)Python編寫(xiě)的計(jì)算器的教程
- 基于python的Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
- Python實(shí)現(xiàn)簡(jiǎn)單的四則運(yùn)算計(jì)算器
- python正則表達(dá)式之作業(yè)計(jì)算器
- Python開(kāi)發(fā)的實(shí)用計(jì)算器完整實(shí)例
- Python實(shí)現(xiàn)的科學(xué)計(jì)算器功能示例
- Python實(shí)現(xiàn)兩款計(jì)算器功能示例
- Python使用wxPython實(shí)現(xiàn)計(jì)算器
- Python Tkinter實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Python實(shí)現(xiàn)的個(gè)人所得稅計(jì)算器示例
相關(guān)文章
python3 實(shí)現(xiàn)在運(yùn)行的時(shí)候隱藏命令窗口
這篇文章主要介紹了python3 實(shí)現(xiàn)在運(yùn)行的時(shí)候隱藏命令窗口方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
numpy 中l(wèi)inspace函數(shù)的使用
本文主要介紹了numpy 中l(wèi)inspace函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python使用smtplib模塊發(fā)送電子郵件的流程詳解
Python中自帶的smtplib模塊可以進(jìn)行基于SMTP協(xié)議的郵件操作,這里我們便總結(jié)了Python使用smtplib模塊發(fā)送電子郵件的流程詳解,并對(duì)一些常見(jiàn)的問(wèn)題給出了解決方法:2016-06-06
基于Python實(shí)現(xiàn)的戀愛(ài)對(duì)話小程序詳解
這篇文章主要介紹了基于Python制作一個(gè)戀愛(ài)對(duì)話小程序,文章詳細(xì)介紹了小程序的實(shí)現(xiàn)過(guò)程,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2022-01-01

