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中字典的合并值相加与异或对比
Jun 04 Python
python学习教程之使用py2exe打包
Sep 24 Python
Python实现XML文件解析的示例代码
Feb 05 Python
python实现两个文件合并功能
Apr 01 Python
python环形单链表的约瑟夫问题详解
Sep 27 Python
python实现socket+threading处理多连接的方法
Jul 23 Python
Python 仅获取响应头, 不获取实体的实例
Aug 21 Python
浅谈Python访问MySQL的正确姿势
Jan 07 Python
Python模块_PyLibTiff读取tif文件的实例
Jan 13 Python
使用keras实现Precise, Recall, F1-socre方式
Jun 15 Python
使用OpenCV去除面积较小的连通域
Jul 05 Python
详解用selenium来下载小姐姐图片并保存
Jan 26 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对表单提交特殊字符的过滤和处理方法汇总
2014/02/18 PHP
php实现建立多层级目录的方法
2014/07/19 PHP
destoon实现调用自增数字从1开始的方法
2014/08/21 PHP
PHP实现搜索相似图片
2015/09/22 PHP
laravel5.1框架基础之Blade模板继承简单使用方法分析
2019/09/05 PHP
浏览器加载、渲染和解析过程黑箱简析
2012/11/29 Javascript
原生javascript模仿win8等待提示圆圈进度条
2014/04/24 Javascript
javascript设计简单的秒表计时器
2020/09/05 Javascript
node.js发送邮件email的方法详解
2017/01/06 Javascript
使用AngularJS 跨站请求如何解决jsonp请求问题
2017/01/16 Javascript
如何将 jQuery 从你的 Bootstrap 项目中移除(取而代之使用Vue.js)
2017/07/17 jQuery
浅谈React Native 中组件的生命周期
2017/09/08 Javascript
利用Three.js如何实现阴影效果实例代码
2017/09/26 Javascript
详解webpack-dev-server的简单使用
2018/04/02 Javascript
node之本地服务器图片上传的方法示例
2019/03/26 Javascript
浅谈js闭包理解
2019/04/01 Javascript
jquery操作checkbox的常用方法总结【附测试源码下载】
2019/06/10 jQuery
vue-router的钩子函数用法实例分析
2019/10/26 Javascript
JavaScript async/await原理及实例解析
2020/12/02 Javascript
python中Genarator函数用法分析
2015/04/08 Python
Python错误: SyntaxError: Non-ASCII character解决办法
2017/06/08 Python
PyQt5主窗口动态加载Widget实例代码
2018/02/07 Python
python实现得到当前登录用户信息的方法
2019/06/21 Python
django连接mysql数据库及建表操作实例详解
2019/12/10 Python
Python爬虫入门有哪些基础知识点
2020/06/02 Python
解决使用Pandas 读取超过65536行的Excel文件问题
2020/11/10 Python
canvas实现扭蛋机动画效果的示例代码
2018/10/17 HTML / CSS
美国专注于健康商品的网站:eVitamins
2017/01/23 全球购物
美国亚洲时尚和美容产品的一站式网上商店:Stylevana
2019/09/05 全球购物
Club Monaco加拿大官网:设计师男女服装
2019/09/29 全球购物
怎样建立和理解非常复杂的声明?例如定义一个包含N 个指向返回 指向字符的指针的函数的指针的数组?
2013/03/19 面试题
运动会广播稿60字
2014/01/15 职场文书
父亲节活动总结
2015/02/12 职场文书
防卫过当辩护词
2015/05/21 职场文书
2015年车间管理工作总结
2015/07/23 职场文书
《珍珠鸟》教学反思
2016/02/16 职场文书