python概率计算器实例分析

yipeiwu_com6年前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!

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python yield使用方法示例

1. iterator叠代器最简单例子应该是数组下标了,且看下面的c++代码: 复制代码 代码如下:int array[10];for ( int i = 0; i < 10; i...

python利用thrift服务读取hbase数据的方法

因工作需要用python通过hbase的thrift服务读取Hbase表数据,发现公司的测试环境还不支持,于是自己动手准备环境,在此我将在安装步骤尽可能描述清楚,旨在给第一次动手安装的朋...

django商品分类及商品数据建模实例详解

基类(商品类及分类类之间共同的字段) class BaseModle(models.Model): name = models.CharField(max_length=32,...

Python基于百度AI的文字识别的示例

Python基于百度AI的文字识别的示例

使用百度AI的文字识别库,做出的调用示例,其中filePath是图片的路径,可以自行传入一张带有文字的图片,进行识别。 下载baidu-aip这个库,可以直接使用pip下载:pip in...

python pycurl验证basic和digest认证的方法

简介 pycurl类似于Python的urllib,但是pycurl是对libcurl的封装,速度更快。 本文使用的是pycurl 7.43.0.1版本。 Apache下配置Basic认...