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中使用urllib2防止302跳转的代码例子
Jul 07 Python
Python列表list解析操作示例【整数操作、字符操作、矩阵操作】
Jul 25 Python
基于并发服务器几种实现方法(总结)
Dec 29 Python
Python实现合并同一个文件夹下所有PDF文件的方法示例
Apr 28 Python
pandas中apply和transform方法的性能比较及区别介绍
Oct 30 Python
python实现名片管理系统项目
Apr 26 Python
Python猴子补丁知识点总结
Jan 05 Python
tensorflow 限制显存大小的实现
Feb 03 Python
Python实现企业微信机器人每天定时发消息实例
Feb 25 Python
Keras 使用 Lambda层详解
Jun 10 Python
Python+unittest+DDT实现数据驱动测试
Nov 30 Python
Python实现老照片修复之上色小技巧
Oct 16 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
解决dede生成静态页和动态页转换的一些问题,及火车采集入库生成动态的办法
2007/03/29 PHP
一款简单实用的php操作mysql数据库类
2014/12/08 PHP
使用PHP uniqid函数生成唯一ID
2015/11/18 PHP
学习thinkphp5.0验证类使用方法
2017/11/16 PHP
PHP自动识别当前使用移动终端
2018/05/21 PHP
一个js封装的不错的选项卡效果代码
2008/02/15 Javascript
javascript XML数据显示为HTML一例
2008/12/23 Javascript
基于jquery的3d效果实现代码
2011/03/23 Javascript
javascript 判断字符串是否包含某字符串及indexOf使用示例
2013/10/18 Javascript
js实现九宫格图片半透明渐显特效的方法
2015/02/16 Javascript
浅谈JavaScript中的Math.atan()方法的使用
2015/06/14 Javascript
JS实现灵巧的下拉导航效果代码
2015/08/25 Javascript
Nodejs初级阶段之express
2015/11/23 NodeJs
微信小程序开发(一) 微信登录流程详解
2017/01/11 Javascript
关于javascript sort()排序你可能忽略的一点理解
2017/07/18 Javascript
Vue+Openlayers自定义轨迹动画
2020/09/24 Javascript
[01:42]DOTA2 – 虚无之灵
2019/08/25 DOTA
Python的面向对象编程方式学习笔记
2016/07/12 Python
Python实现App自动签到领取积分功能
2018/09/29 Python
python 使用shutil复制图片的例子
2019/12/13 Python
Python类中self参数用法详解
2020/02/13 Python
HTML5 Convas APIs方法详解
2015/04/24 HTML / CSS
英国蛋糕装饰用品一站式商店:Craft Company
2019/03/18 全球购物
俄罗斯连接商品和买家的在线平台:goods.ru
2020/11/30 全球购物
食堂个人先进事迹
2014/01/22 职场文书
“学雷锋活动月”总结
2014/03/09 职场文书
技术经济专业求职信
2014/09/03 职场文书
纪念九一八事变演讲稿:青少年应树立远大理想
2014/09/14 职场文书
授权委托书怎么写
2014/09/25 职场文书
2014年大学教师工作总结
2014/12/02 职场文书
2015教师年度考核评语
2015/03/25 职场文书
房地产项目合作意向书
2015/05/08 职场文书
2015年全民创业工作总结
2015/07/23 职场文书
2016年教师政治思想表现评语
2015/12/02 职场文书
2016年七夕爱情寄语
2015/12/04 职场文书
使用Redis实现分布式锁的方法
2022/06/16 Redis