python概率计算器实例分析

yipeiwu_com6年前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操作SQLite数据库过程解析

SQLite是一款轻型的数据库,是遵守ACID的关系型数据库管理系统。 不像常见的客户-服务器范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分。...

Numpy数据类型转换astype,dtype的方法

1、查看数据类型 In [11]: arr = np.array([1,2,3,4,5]) In [12]: arr Out[12]: array([1, 2, 3, 4, 5])...

Python3实现的旋转矩阵图像算法示例

本文实例讲述了Python3实现的旋转矩阵图像算法。分享给大家供大家参考,具体如下: 问题: 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 方案一:先...

python中virtualenvwrapper安装与使用

virtualenv与virtualenvwrapper 当涉及到python项目开发时为了不污染全局环境,通常都会使用环境隔离管理工具virtualenv与virtualenvwra...

matplotlib绘图实例演示标记路径

matplotlib绘图实例演示标记路径

标记路径 演示效果: 实例代码 import matplotlib.pyplot as plt import matplotlib.path as mpath import num...