python概率计算器实例分析


Posted in Python onMarch 25, 2015

本文实例讲述了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基础教程之常用运算符
Aug 29 Python
Python入门学习之字符串与比较运算符
Oct 12 Python
Python实现SMTP发送邮件详细教程
Mar 02 Python
Python对CSV、Excel、txt、dat文件的处理
Sep 18 Python
利用Python实现原创工具的Logo与Help
Dec 03 Python
python redis 删除key脚本的实例
Feb 19 Python
简单了解python关系(比较)运算符
Jul 08 Python
python内置函数sorted()用法深入分析
Oct 08 Python
配置python的编程环境之Anaconda + VSCode的教程
Mar 29 Python
用于ETL的Python数据转换工具详解
Jul 21 Python
Python性能分析工具py-spy原理用法解析
Jul 27 Python
在 Windows 下搭建高效的 django 开发环境的详细教程
Jul 27 Python
python编写的最短路径算法
Mar 25 #Python
python实现挑选出来100以内的质数
Mar 24 #Python
Python 的 Socket 编程
Mar 24 #Python
python获取标准北京时间的方法
Mar 24 #Python
python实现定时同步本机与北京时间的方法
Mar 24 #Python
Python随机生成一个6位的验证码代码分享
Mar 24 #Python
python判断字符串是否包含子字符串的方法
Mar 24 #Python
You might like
如何写php程序?
2006/12/08 PHP
php内核解析:PHP中的哈希表
2014/01/30 PHP
一个不易被发现的PHP后门代码解析
2014/07/05 PHP
php基础教程
2015/08/26 PHP
WordPress中使主题支持小工具以及添加插件启用函数
2015/12/22 PHP
php常用图片处理类
2016/03/16 PHP
PHP入门教程之表单与验证实例详解
2016/09/11 PHP
微信小程序 消息推送php服务器验证实例详解
2017/03/30 PHP
PNGHandler-借助JS让PNG图在IE下实现透明(包括背景图)
2007/08/31 Javascript
编写Js代码要注意的几条规则
2010/09/10 Javascript
JS分页控件 可用于无刷新分页
2013/07/23 Javascript
js点击文本框后才加载验证码实例代码
2015/10/20 Javascript
javascript日期验证之输入日期大于等于当前日期
2015/12/13 Javascript
javascript和jquery实现用户登录验证
2016/05/04 Javascript
深入理解Angularjs中的$resource服务
2016/12/31 Javascript
浅谈Vuejs中nextTick()异步更新队列源码解析
2017/12/31 Javascript
react中fetch之cors跨域请求的实现方法
2018/03/14 Javascript
使用Vue构建可重用的分页组件
2018/03/26 Javascript
vue.js多页面开发环境搭建过程
2019/04/24 Javascript
python用装饰器自动注册Tornado路由详解
2017/02/14 Python
Python之自动获取公网IP的实例讲解
2017/10/01 Python
Python切片操作深入详解
2018/07/27 Python
详解使用Python下载文件的几种方法
2019/10/13 Python
python使用pip安装SciPy、SymPy、matplotlib教程
2019/11/20 Python
Python selenium 自动化脚本打包成一个exe文件(推荐)
2020/01/14 Python
Python魔术方法专题
2020/06/19 Python
Jupyter Notebook 远程访问配置详解
2021/01/11 Python
Python 内存管理机制全面分析
2021/01/16 Python
利用CSS3实现毛玻璃效果示例源码
2016/09/25 HTML / CSS
探究 canvas 绘图中撤销(undo)功能的实现方式详解
2018/05/17 HTML / CSS
小学安全教育材料
2014/02/17 职场文书
实习介绍信模板
2015/01/30 职场文书
教师求职自荐信
2015/03/26 职场文书
傅雷家书读书笔记
2015/06/29 职场文书
彻底理解golang中什么是nil
2021/04/29 Golang
使用php的mail()函数实现发送邮件功能
2021/06/03 PHP