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中的正则表达式的用法
Apr 09 Python
python实现用户管理系统
Jan 10 Python
python spyder中读取txt为图片的方法
Apr 27 Python
如何使用Python标准库进行性能测试
Jun 25 Python
python程序运行进程、使用时间、剩余时间显示功能的实现代码
Jul 11 Python
基于python操作ES实例详解
Nov 16 Python
利用Python裁切tiff图像且读取tiff,shp文件的实例
Mar 10 Python
详解python中groupby函数通俗易懂
May 14 Python
python基于socket函数实现端口扫描
May 28 Python
python爬虫---requests库的用法详解
Sep 28 Python
Pycharm中如何关掉python console
Oct 27 Python
Ubuntu16安装Python3.9的实现步骤
Dec 15 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
PHILIPS L4X25T电路分析和打理
2021/03/02 无线电
用文本文件制作留言板提示(下)
2006/10/09 PHP
codeigniter上传图片不能正确识别图片类型问题解决方法
2014/07/25 PHP
ThinkPHP框架安全实现分析
2016/03/14 PHP
浅谈PHP中类和对象的相关函数
2017/04/26 PHP
PHP基于curl post实现发送url及相关中文乱码问题解决方法
2017/11/25 PHP
用Javascript同时提交多个Web表单的方法
2009/12/26 Javascript
javascript实现二级级联菜单的简单制作
2015/11/19 Javascript
JavaScript动态创建form表单并提交的实现方法
2015/12/10 Javascript
很不错的两款Bootstrap Icon图标选择组件
2016/01/28 Javascript
JS实现刷新父页面不弹出提示框的方法
2016/06/22 Javascript
详解Sea.js中Module.exports和exports的区别
2017/02/12 Javascript
vue2.0实战之使用vue-cli搭建项目(2)
2017/03/27 Javascript
Vue v2.4中新增的$attrs及$listeners属性使用教程
2018/01/08 Javascript
vue2.0路由切换后页面滚动位置不变BUG的解决方法
2018/03/14 Javascript
如何在 JavaScript 中更好地利用数组
2018/09/27 Javascript
javascript中如何判断类型汇总
2019/05/14 Javascript
js+canvas实现图片格式webp/png/jpeg在线转换
2020/08/22 Javascript
[01:05:32]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第一局
2016/03/04 DOTA
用Python解决计数原理问题的方法
2016/08/04 Python
Django Web开发中django-debug-toolbar的配置以及使用
2018/05/06 Python
[原创]Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】
2018/10/29 Python
PyQt弹出式对话框的常用方法及标准按钮类型
2019/02/27 Python
Python空间数据处理之GDAL读写遥感图像
2019/08/01 Python
python查看数据类型的方法
2019/10/12 Python
python主线程与子线程的结束顺序实例解析
2019/12/17 Python
韩国江南富人区高端时尚百货商场:Galleria(格乐丽雅)
2018/03/27 全球购物
Vector, ArrayList, HashTable, HashMap哪些是线程安全的,哪些不是
2015/10/12 面试题
关于打架的检讨书
2014/01/17 职场文书
太太口服液广告词
2014/03/20 职场文书
工程技术负责人岗位职责
2015/04/13 职场文书
2016创先争优活动党员公开承诺书
2016/03/24 职场文书
Apache压力测试工具的安装使用
2021/03/31 Servers
世界各国短波电台对东亚播送时间频率表(SW)
2021/06/28 无线电
HashMap实现保存两个key相同的数据
2021/06/30 Java/Android
idea 在springboot中使用lombok插件的方法
2021/08/02 Java/Android