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的chardet库获得文件编码并修改编码
Jan 22 Python
python实现二维码扫码自动登录淘宝
Dec 27 Python
读取本地json文件,解析json(实例讲解)
Dec 06 Python
PyGame贪吃蛇的实现代码示例
Nov 21 Python
python如何实现代码检查
Jun 28 Python
Python Django 命名空间模式的实现
Aug 09 Python
解决python多行注释引发缩进错误的问题
Aug 23 Python
python 实现绘制整齐的表格
Nov 18 Python
Python HTMLTestRunner可视化报告实现过程解析
Apr 10 Python
django 装饰器 检测登录状态操作
Jul 02 Python
Django如何实现防止XSS攻击
Oct 13 Python
如何在pycharm中快捷安装pip命令(如pygame)
May 31 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
使用Thinkphp框架开发移动端接口
2015/08/05 PHP
PHP入门教程之操作符与控制结构流程详解
2016/09/09 PHP
php实现的错误处理封装类实例
2017/06/20 PHP
PHP封装的page分页类定义与用法完整示例
2018/12/24 PHP
PHP安全之register_globals的on和off的区别
2020/07/23 PHP
JS 实现完美include载入实现代码
2010/08/05 Javascript
JQuery中getJSON的使用方法
2010/12/13 Javascript
jquery ajax jsonp跨域调用实例代码
2013/12/11 Javascript
Bootstrap菜单按钮及导航实例解析
2016/09/09 Javascript
Javascript中arguments对象的详解与使用方法
2016/10/04 Javascript
jQuery NProgress.js加载进度插件的简单使用方法
2018/01/31 jQuery
JS实现的合并多个数组去重算法示例
2018/04/11 Javascript
Node.js应用设置安全的沙箱环境
2018/04/23 Javascript
Vue无限滑动周选择日期的组件的示例代码
2018/07/18 Javascript
vue用递归组件写树形控件的实例代码
2018/07/19 Javascript
Javascript组合继承方法代码实例解析
2020/04/02 Javascript
详解Vue之事件处理
2020/07/10 Javascript
Vue+Java 通过websocket实现服务器与客户端双向通信操作
2020/09/22 Javascript
python实现的守护进程(Daemon)用法实例
2015/06/02 Python
在Linux命令行终端中使用python的简单方法(推荐)
2017/01/23 Python
Python实现感知器模型、两层神经网络
2017/12/19 Python
Flask 让jsonify返回的json串支持中文显示的方法
2018/03/26 Python
利用Python进行数据可视化常见的9种方法!超实用!
2018/07/11 Python
Sanic框架异常处理与中间件操作实例分析
2018/07/16 Python
Python基于QQ邮箱实现SSL发送
2020/04/26 Python
python实现在线翻译
2020/06/18 Python
css实例教程 一款纯css3实现的超炫动画背画特效
2014/11/05 HTML / CSS
线程同步的方法
2016/11/23 面试题
大学生求职简历的自我评价
2013/10/21 职场文书
西门豹教学反思
2014/02/04 职场文书
院领导写的就业推荐信
2014/03/09 职场文书
2014年五四青年节演讲稿范文
2014/04/22 职场文书
委托书的写法
2014/08/30 职场文书
党的群众路线教育实践活动批评与自我批评范文
2014/10/16 职场文书
辞职信的写法
2015/02/27 职场文书
某某店铺的开业庆典主持词范本
2019/11/25 职场文书