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英文文本分词(无空格)模块wordninja的使用实例

在NLP中,数据清洗与分词往往是很多工作开始的第一步,大多数工作中只有中文语料数据需要进行分词,现有的分词工具也已经有了很多了,这里就不再多介绍了。英文语料由于其本身存在空格符所以无需跟...

Python数据库小程序源代码

源代码: # dict1 是 字典 , 用来对应相应元素的下标,我们将文件转成列表,对应的也就是文件的下标,通过下标来找文件元素 dict1 = {'sort':0 , 'name...

python实现代码统计器

本文实例为大家分享了python中的代码行数统计,供大家参考,具体内容如下 思路:统计文件中代码的总行数减去空行单行注释以及多行注释 功能: 1.获取文件内容的总行数 2.排除空行 单行...

Python3利用Dlib19.7实现摄像头人脸识别的方法

Python3利用Dlib19.7实现摄像头人脸识别的方法

0.引言 利用python开发,借助Dlib库捕获摄像头中的人脸,提取人脸特征,通过计算欧氏距离来和预存的人脸特征进行对比,达到人脸识别的目的; 可以自动从摄像头中抠取人脸图片存储到本地...

python批量解压zip文件的方法

这是一个用python写解压大量zip脚本的说明,本人新手一个,希望能对各位有所启发。 首先要注意的,在运行自己的脚本之前一定先备份或者复制出一些样本进行测试,不然出错会很麻烦; 之后我...