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将图片文件转换成base64编码的方法
Mar 14 Python
Python 详解基本语法_函数_返回值
Jan 22 Python
如何使用VSCode愉快的写Python于调试配置步骤
Apr 06 Python
python顺序的读取文件夹下名称有序的文件方法
Jul 11 Python
自学python的建议和周期预算
Jan 30 Python
Python中函数的基本定义与调用及内置函数详解
May 13 Python
django 信号调度机制详解
Jul 19 Python
Django 后台获取文件列表 InMemoryUploadedFile的例子
Aug 07 Python
tensorflow 固定部分参数训练,只训练部分参数的实例
Jan 20 Python
Pandas时间序列:时期(period)及其算术运算详解
Feb 25 Python
Python+Appium实现自动化清理微信僵尸好友的方法
Feb 04 Python
分享Python异步爬取知乎热榜
Apr 12 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
Windows下PHP的任意文件执行漏洞
2006/10/09 PHP
Dedecms常用函数解析
2008/02/01 PHP
php 将excel导入mysql
2009/11/09 PHP
php checkbox 取值详细说明
2010/08/19 PHP
php excel reader读取excel内容存入数据库实现代码
2012/12/06 PHP
探讨:如何使用PHP实现计算两个日期间隔的年、月、周、日数
2013/06/13 PHP
PHP实现的英文名字全拼随机排号脚本
2014/07/04 PHP
PHP中使用localhost连接Mysql不成功的解决方法
2014/08/20 PHP
Laravel 5 学习笔记
2015/03/06 PHP
PHP常见的序列化与反序列化操作实例分析
2019/10/28 PHP
准确获得页面、窗口高度及宽度的JS
2006/11/26 Javascript
js计算字符串长度包含的中文是utf8格式
2013/10/15 Javascript
使用Js让Html中特殊字符不被转义
2013/11/05 Javascript
Javascript中call与apply的学习笔记
2014/09/22 Javascript
JQuery.get提交页面不跳转的解决方法
2015/01/13 Javascript
js实现从中间开始往上下展开网页窗口的方法
2015/03/02 Javascript
Javascript闭包(Closure)详解
2015/05/05 Javascript
JS实现仿新浪微博发布内容为空时提示功能代码
2015/08/19 Javascript
JavaScript学习小结之使用canvas画“哆啦A梦”时钟
2016/07/24 Javascript
关于JS模块化的知识点分享
2019/10/16 Javascript
[19:15]DK战队纪录片
2014/09/02 DOTA
Windows下安装python2.7及科学计算套装
2015/03/05 Python
Python CSV模块使用实例
2015/04/09 Python
Python JSON格式数据的提取和保存的实现
2019/03/22 Python
如何将tensorflow训练好的模型移植到Android (MNIST手写数字识别)
2020/04/22 Python
使用matplotlib动态刷新指定曲线实例
2020/04/23 Python
python安装第三方库如xlrd的方法
2020/10/31 Python
Pycharm创建python文件自动添加日期作者等信息(步骤详解)
2021/02/03 Python
CSS3实现时间轴效果
2016/07/11 HTML / CSS
美国当红的名品折扣网:Gilt Groupe
2016/08/15 全球购物
UGG雪地靴荷兰官网:UGG荷兰
2016/09/09 全球购物
Ryderwear美国官网:澳大利亚高端健身训练装备品牌
2018/04/24 全球购物
表演方阵解说词
2014/02/08 职场文书
2014年志愿者工作总结
2014/11/20 职场文书
2014年司机工作总结
2014/11/21 职场文书
董事长新年致辞
2015/07/29 职场文书