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实现目录树生成示例
Mar 28 Python
解决DataFrame排序sort的问题
Jun 07 Python
如何利用python制作时间戳转换工具详解
Sep 12 Python
Pandas_cum累积计算和rolling滚动计算的用法详解
Jul 04 Python
python判断单向链表是否包括环,若包含则计算环入口的节点实例分析
Oct 23 Python
Python笔记之代理模式
Nov 20 Python
pytorch GAN伪造手写体mnist数据集方式
Jan 10 Python
Python chardet库识别编码原理解析
Feb 18 Python
在jupyter notebook 添加 conda 环境的操作详解
Apr 10 Python
Tensorflow--取tensorf指定列的操作方式
Jun 30 Python
Python-openpyxl表格读取写入的案例详解
Nov 02 Python
python语言中pandas字符串分割str.split()函数
Aug 05 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之数据库操作详解及乱码解决!
2007/01/02 PHP
php打开文件fopen函数的使用说明
2013/07/05 PHP
PHP内核探索:变量概述
2014/01/30 PHP
PHP中的日期加减方法示例
2014/08/21 PHP
分析PHP中单双引号的误区和双引号小隐患
2016/07/19 PHP
php实现连接access数据库并转txt写入的方法
2017/02/08 PHP
基于laravel where的高级使用方法
2019/10/10 PHP
jQuery下扩展插件和拓展函数的写法(匿名函数使用的典型例子)
2010/10/20 Javascript
jQuery源码分析-03构造jQuery对象-源码结构和核心函数
2011/11/14 Javascript
javascript闭包入门示例
2014/04/30 Javascript
Node.js静态文件服务器改进版
2016/01/10 Javascript
Bootstrap Paginator分页插件使用方法详解
2016/05/30 Javascript
vue.js入门教程之绑定class和style样式
2016/09/02 Javascript
简单的JS控制button颜色随点击更改的实现方法
2017/04/17 Javascript
BootStrap给table表格的每一行添加一个按钮事件
2017/09/07 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
Angular angular-file-upload文件上传的示例代码
2018/08/23 Javascript
利用jqgrid实现上移下移单元格功能
2018/11/07 Javascript
Node.js实现简单的爬取的示例代码
2019/06/25 Javascript
分享8个JavaScript库可更好地处理本地存储
2020/10/12 Javascript
Python解析xml中dom元素的方法
2015/03/12 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
2018/03/13 Python
python turtle 绘制太极图的实例
2019/12/18 Python
Keras Convolution1D与Convolution2D区别说明
2020/05/22 Python
Python3以GitHub为例来实现模拟登录和爬取的实例讲解
2020/07/30 Python
python爬虫使用正则爬取网站的实现
2020/08/03 Python
python实现三壶谜题的示例详解
2020/11/02 Python
css3中新增的样式使用示例附效果图
2014/08/19 HTML / CSS
阿里巴巴国际站:Alibaba.com
2016/07/21 全球购物
澳大利亚领先的武术用品和健身器材供应商:SMAI
2019/03/24 全球购物
西铁城美国官方网站:Citizen Watch美国
2019/11/08 全球购物
工作自我评价怎么写
2014/01/29 职场文书
同学聚会策划方案
2014/06/06 职场文书
学生检讨书范文
2014/10/30 职场文书
2014年司法局工作总结
2014/12/11 职场文书
浅析CSS在DevTools 中架构演变
2021/10/05 HTML / CSS