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中使用列表生成式的教程
Apr 27 Python
Python的多态性实例分析
Jul 07 Python
谈谈Python进行验证码识别的一些想法
Jan 25 Python
详解Python的Twisted框架中reactor事件管理器的用法
May 25 Python
利用标准库fractions模块让Python支持分数类型的方法详解
Aug 11 Python
python 读入多行数据的实例
Apr 19 Python
利用Python实现在同一网络中的本地文件共享方法
Jun 04 Python
Python OpenCV处理图像之滤镜和图像运算
Jul 10 Python
django drf框架中的user验证以及JWT拓展的介绍
Aug 12 Python
python3-flask-3将信息写入日志的实操方法
Nov 12 Python
Pytorch 实现sobel算子的卷积操作详解
Jan 10 Python
Python requests库参数提交的注意事项总结
Mar 29 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
星际争霸, 教主第一视角, ZvT经典龙蛇演义
2020/03/02 星际争霸
PHP 输出缓存详解
2009/06/20 PHP
通过PHP CLI实现简单的数据库实时监控调度
2009/07/01 PHP
ecshop实现smtp发送邮件
2015/02/03 PHP
利用php生成验证码
2017/02/23 PHP
PHP7.1实现的AES与RSA加密操作示例
2018/06/15 PHP
laravel框架中间件 except 和 only 的用法示例
2019/07/12 PHP
ext form 表单提交数据的方法小结
2008/08/08 Javascript
jquery之Document元素选择器篇
2008/08/14 Javascript
Javascript查询DBpedia小应用实例学习
2013/03/07 Javascript
javascript中HTMLDOM操作详解
2014/12/11 Javascript
[原创]JQuery 在表单提交之前修改 提交的值
2016/04/14 Javascript
JS数组返回去重后数据的方法解析
2017/01/03 Javascript
vue 实现的树形菜的实例代码
2018/03/19 Javascript
jQuery实现每隔一段时间自动更换样式的方法分析
2018/05/03 jQuery
js中arguments对象的深入理解
2019/05/14 Javascript
jquery+php后台实现省市区联动功能示例
2019/05/23 jQuery
vue实现树形结构样式和功能的实例代码
2019/10/15 Javascript
Vue是怎么渲染template内的标签内容的
2020/06/05 Javascript
[04:29]【TI9采访】OG.N0tail在胜者组决赛后接受采访
2019/08/25 DOTA
浅述python中argsort()函数的实例用法
2017/03/30 Python
详谈Python高阶函数与函数装饰器(推荐)
2017/09/30 Python
python实现输入数字的连续加减方法
2018/06/22 Python
详解利用python+opencv识别图片中的圆形(霍夫变换)
2019/07/01 Python
Python在OpenCV里实现极坐标变换功能
2019/09/02 Python
Python 分布式缓存之Reids数据类型操作详解
2020/06/24 Python
Pytorch生成随机数Tensor的方法汇总
2020/09/09 Python
Web页面中八种创建多列等高(等高列布局)的实现技术
2012/12/24 HTML / CSS
越南母婴用品购物网站:Kids Plaza
2020/04/09 全球购物
副总经理岗位职责
2014/03/16 职场文书
网络营销策划方案
2014/06/04 职场文书
师范毕业生求职信
2014/07/11 职场文书
外科护士长工作总结
2015/08/12 职场文书
感恩主题班会教案
2015/08/12 职场文书
少儿励志名言(80句)
2019/08/14 职场文书
python和C/C++混合编程之使用ctypes调用 C/C++的dll
2022/04/29 Python