python概率计算器源码

在工作过程中中,把开发过程中比较常用的内容做个备份,下面内容内容是关于python概率计算器的内容,应该能对各朋友也有用途。

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!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,081评论 0 23
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • 世界那么大我想去看看。 我最喜欢自驾游,首先我要学好地理,其次有一颗敢于冒险的心,最后积累经验,等待机会。不需要多...
    白菜圈阅读 258评论 0 0
  • 练习步骤: 1. 观察:发生了什么事情? 今天看声临其境的决赛,为声音大咖们的百变音色折服。看到这么优秀的人这么努...
    张露deer阅读 128评论 0 0
  • 十二余载游学海,千军万马独木桥;沙场一去不复返,考场一日徒留憾。左右徘徊选高校,捻眉捋须填志愿;志愿系统已瘫痪,苦...
    葡萄月令阅读 199评论 0 0