python概率计算器实例分析

yipeiwu_com5年前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线程创建和终止实例代码

python主要是通过thread和threading这两个模块来实现多线程支持。 python的thread模块是比較底层的模块,python的threading模块是对thread做...

python机器学习库xgboost的使用

python机器学习库xgboost的使用

1.数据读取 利用原生xgboost库读取libsvm数据 import xgboost as xgb data = xgb.DMatrix(libsvm文件) 使用sk...

Mac中Python 3环境下安装scrapy的方法教程

Mac中Python 3环境下安装scrapy的方法教程

前言 最近抽空想学习一下python的爬虫框架scrapy,在mac下安装的时候遇到了问题,逐一解决了问题,分享一下,话不多说了,来一起看看详细的介绍吧。 步骤如下: 1. 从官网 下载...

Python3删除排序数组中重复项的方法分析

本文实例讲述了Python3删除排序数组中重复项的方法。分享给大家供大家参考,具体如下: 给定一个排序数组,你需要在[原地]删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新...

python3.6+opencv3.4实现鼠标交互查看图片像素

python3.6+opencv3.4实现鼠标交互查看图片像素

在利用opencv进行图片处理时,经常需要查看图片关心区域或位置的像素数值,苦于没有应手的小软件,我用python3.6+opencv3.4简单编制一个小工具,供大家使用。 流程 1...