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映射列表实例分析
Jan 26 Python
使用Python来开发Markdown脚本扩展的实例分享
Mar 04 Python
python Crypto模块的安装与使用方法
Dec 21 Python
Python3处理HTTP请求的实例
May 10 Python
pyqt5使用按钮进行界面的跳转方法
Jun 19 Python
pyinstaller打包单个exe后无法执行错误的解决方法
Jun 21 Python
PyCharm更改字体和界面样式的方法步骤
Sep 27 Python
centos7中安装python3.6.4的教程
Dec 11 Python
基于python3抓取pinpoint应用信息入库
Jan 08 Python
运行tensorflow python程序,限制对GPU和CPU的占用操作
Feb 06 Python
解决Tensorboard 不显示计算图graph的问题
Feb 15 Python
Python实现手势识别
Oct 21 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
【COS正片】蕾姆睡衣cos,纯洁可爱被治愈了 cn名濑弥七
2020/03/02 日漫
PHP+MYSQL 出现乱码的解决方法
2008/08/08 PHP
iOS自定义提示弹出框实现类似UIAlertView的效果
2016/11/16 PHP
thinkPHP简单实现多个子查询语句的方法
2016/12/05 PHP
php查看一个变量的占用内存的实例代码
2020/03/29 PHP
Thinkphp 框架扩展之应用模式实现方法分析
2020/04/27 PHP
PHP7 新增常量
2021/03/09 PHP
表单提交时自动复制内容到剪贴板的js代码
2007/03/16 Javascript
jQuery 中关于CSS操作部分使用说明
2007/06/10 Javascript
Chrome Form多次提交表单问题的解决方法
2011/05/09 Javascript
读jQuery之十三 添加事件和删除事件的核心方法
2011/08/23 Javascript
javascript小组件 原生table排序表格脚本(兼容ie firefox opera chrome)
2012/07/25 Javascript
iframe异步加载实现点击左边菜单加载右边内容实例讲解
2013/03/04 Javascript
使用js实现雪花飘落效果
2013/08/26 Javascript
jquery删除提示框弹出是否删除对话框
2014/01/07 Javascript
js中的caller和callee属性介绍和例子
2014/06/07 Javascript
json字符串之间的相互转换示例代码
2014/08/21 Javascript
谈谈encodeURI和encodeURIComponent以及escape的区别与应用
2015/11/24 Javascript
基于javascript 显式转换与隐式转换(详解)
2017/12/15 Javascript
详解组件库的webpack构建速度优化
2018/06/18 Javascript
Echarts之悬浮框中的数据排序问题
2018/11/08 Javascript
Python标准库之sqlite3使用实例
2014/11/25 Python
Python多进程multiprocessing用法实例分析
2017/08/18 Python
python smtplib发送带附件邮件小程序
2018/05/22 Python
python实现对求解最长回文子串的动态规划算法
2018/06/02 Python
Python爬取数据并写入MySQL数据库的实例
2018/06/21 Python
Pandas实现dataframe和np.array的相互转换
2019/11/30 Python
利用CSS3的transition属性实现滑动效果
2015/08/05 HTML / CSS
Boden美国官网:英伦原创时装品牌
2017/07/03 全球购物
实现strstr功能,即在父串中寻找子串首次出现的位置
2016/08/05 面试题
Java编程面试题
2016/04/04 面试题
建筑人员岗位职责
2013/12/25 职场文书
文化宣传方案
2014/03/13 职场文书
2014年设备管理工作总结
2014/11/26 职场文书
详解Python自动化之文件自动化处理
2021/06/21 Python
CSS 鼠标点击拖拽效果的实现代码
2022/12/24 HTML / CSS