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深入学习之内存管理
Aug 31 Python
使用Python的Treq on Twisted来进行HTTP压力测试
Apr 16 Python
Ruby使用eventmachine为HTTP服务器添加文件下载功能
Apr 20 Python
基于Python函数的作用域规则和闭包(详解)
Nov 29 Python
Python实现破解12306图片验证码的方法分析
Dec 29 Python
Python多层装饰器用法实例分析
Feb 09 Python
python同步windows和linux文件
Aug 29 Python
python实现代码统计器
Sep 19 Python
Python 3.8正式发布,来尝鲜这些新特性吧
Oct 15 Python
Python os模块常用方法和属性总结
Feb 20 Python
新手必备Python开发环境搭建教程
May 28 Python
Python虚拟环境virtualenv是如何使用的
Jun 20 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
jQuery+php实现ajax文件即时上传的详解
2013/06/17 PHP
PHP删除数组中空值的方法介绍
2014/04/14 PHP
使用PHPMailer实现邮件发送代码分享
2014/10/23 PHP
php使用 readfile() 函数设置文件大小大小的方法
2017/08/11 PHP
Javascript 文件夹选择框的两种解决方案
2009/07/01 Javascript
js保存当前路径(cookies记录)
2010/12/14 Javascript
用jQuery中的ajax分页实现代码
2011/09/20 Javascript
jQuery拖拽通过八个点改变div大小
2020/11/29 Javascript
AngularJS实现网站换肤实例
2021/02/19 Javascript
JavaScript 中调用 Kotlin 方法实例详解
2017/06/09 Javascript
微信小程序非swiper组件实现的自定义伪3D轮播图效果示例
2018/12/11 Javascript
layer弹出层倒计时关闭的实现方法
2019/09/27 Javascript
使用Python中的greenlet包实现并发编程的入门教程
2015/04/16 Python
使用Python的Tornado框架实现一个一对一聊天的程序
2015/04/25 Python
Python实现多线程抓取妹子图
2015/08/08 Python
一条命令解决mac版本python IDLE不能输入中文问题
2018/05/15 Python
对Python 语音识别框架详解
2018/12/24 Python
浅谈python图片处理Image和skimage的区别
2019/08/04 Python
Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例
2020/02/05 Python
Python GUI编程学习笔记之tkinter事件绑定操作详解
2020/03/30 Python
通过python 执行 nohup 不生效的解决
2020/04/16 Python
Python自动发送和收取邮件的方法
2020/08/12 Python
Python的logging模块基本用法
2020/12/24 Python
Python3利用openpyxl读写Excel文件的方法实例
2021/02/03 Python
详解HTML5 录音的踩坑之旅
2017/12/26 HTML / CSS
大学自荐信
2013/12/12 职场文书
银行见习期自我鉴定
2014/01/29 职场文书
给老婆大人的检讨书
2014/02/24 职场文书
《落花生》教学反思
2014/02/25 职场文书
cf收人广告词大全
2014/03/14 职场文书
销售个人求职信范文
2014/04/28 职场文书
做一个有道德的人演讲稿
2014/05/14 职场文书
2014党员批评和自我批评思想汇报
2014/09/21 职场文书
Vue实现导入Excel功能步骤详解
2021/07/03 Vue.js
详解Vue slot插槽
2021/11/20 Vue.js
为什么MySQL8新特性会修改自增主键属性
2022/04/18 MySQL