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 with的用法
Aug 22 Python
pygame学习笔记(4):声音控制
Apr 15 Python
python实现给数组按片赋值的方法
Jul 28 Python
解决Python中字符串和数字拼接报错的方法
Oct 23 Python
python字符串str和字节数组相互转化方法
Mar 18 Python
Python开发的HTTP库requests详解
Aug 29 Python
Python中请不要再用re.compile了
Jun 30 Python
Python字典对象实现原理详解
Jul 01 Python
python卸载后再次安装遇到的问题解决
Jul 10 Python
Python 读取有公式cell的结果内容实例方法
Feb 17 Python
Python爬虫之Spider类用法简单介绍
Aug 04 Python
pycharm中选中一个单词替换所有重复单词的实现方法
Nov 17 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下使用SMTP发邮件的代码
2008/01/10 PHP
漂亮的仿flash菜单,来自蓝色经典
2006/06/26 Javascript
Javascript String.replace的妙用
2009/09/08 Javascript
jquery 应用代码 方便的排序功能
2010/02/06 Javascript
用jQuery实现一些导航条切换,显示隐藏的实例代码
2013/06/08 Javascript
js实时获取系统当前时间实例代码
2013/06/28 Javascript
使用jQuery解决IE与FireFox下createElement方法的差异
2013/11/14 Javascript
Jquery之Bind方法参数传递与接收的三种方法
2014/06/24 Javascript
用javascript读取xml文件读取节点数据
2014/08/12 Javascript
jQuery使用prepend()方法在元素前添加内容用法实例
2015/03/26 Javascript
总结JavaScript设计模式编程中的享元模式使用
2016/05/21 Javascript
JS简单设置下拉选择框默认值的方法
2016/08/20 Javascript
AngularJS ng-style中使用filter
2016/09/21 Javascript
微信小程序 tabs选项卡效果的实现
2017/01/05 Javascript
深入理解Javascript箭头函数中的this
2017/02/13 Javascript
使用3D引擎threeJS实现星空粒子移动效果
2020/09/13 Javascript
vue+vuex+axios+echarts画一个动态更新的中国地图的方法
2017/12/19 Javascript
React学习笔记之高阶组件应用
2018/06/02 Javascript
JavaScript实现的简单Tab点击切换功能示例
2018/07/06 Javascript
基于AngularJs select绑定数字类型的问题
2018/10/08 Javascript
使用layui的router来进行传参的实现方法
2019/09/06 Javascript
Python的批量远程管理和部署工具Fabric用法实例
2015/01/23 Python
Python代码调试的几种方法总结
2015/04/15 Python
Python实现读写INI配置文件的方法示例
2018/06/09 Python
Centos部署django服务nginx+uwsgi的方法
2019/01/02 Python
使用python实现简单五子棋游戏
2019/06/18 Python
详解Python3 pickle模块用法
2019/09/16 Python
如何在sublime编辑器中安装python
2020/05/20 Python
HTML5 video标签(播放器)学习笔记(二):播放控制
2015/04/24 HTML / CSS
加拿大购物频道:The Shopping Channel
2016/07/21 全球购物
俄罗斯汽车零件和配件在线商店:CarvilleShop
2019/11/29 全球购物
学前教育专业毕业生自荐信
2013/10/03 职场文书
高中毕业生个人自我鉴定
2013/11/24 职场文书
师范生个人推荐信
2013/11/29 职场文书
2014法院干警廉洁警示教育思想汇报
2014/09/13 职场文书
2014年数学教研组工作总结
2014/12/06 职场文书