python概率计算器实例分析

yipeiwu_com5年前Python基础

本文实例讲述了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里使用正则表达式的组嵌套实例详解 由于组本身是一个完整的正则表达式,所以可以将组嵌套在其他组中,以构建更复杂的表达式。下面的例子,就是进行组嵌套的例子: #python...

Python实用日期时间处理方法汇总

原则, 以datetime为中心, 起点或中转, 转化为目标对象, 涵盖了大多数业务场景中需要的日期转换处理 步骤: 1. 掌握几种对象及其关系 2. 了解每类对象的基本操作方法 3....

Django 实现xadmin后台菜单改为中文

应用目录下apps.py class OperationConfig(AppConfig): name = 'operation' verbose_name = u"用户操作...

Python内存管理实例分析

Python内存管理实例分析

本文实例讲述了Python内存管理。分享给大家供大家参考,具体如下: a = 1 a是引用,1是对象。Python缓存整数和短字符串,对象只有一份,但长字符串和其他对象(列表字...

如何优雅地改进Django中的模板碎片缓存详解

如何优雅地改进Django中的模板碎片缓存详解

前言 本文主页给大家介绍了关于如何改进Django中模板碎片缓存的相关内容,关于Django模板碎片缓存大家可以先看看这篇文章,下面话不多说了,来一起看看详细的介绍吧 起步 Djang...