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编译成.so文件进行加密后调用的实现

pyc的破解相对容易,使用cython将python文件编译成.so文件,能在一定程度上增强python源码的私密性。 编译成.so文件 环境准备:cython 测试脚本准备:test....

使用python将时间转换为指定的格式方法

时间处理是在进行数据挖掘时很重要的一个方面,在参加比赛的时候很多比赛训练集给的时间和你最终要提交的时间格式是不同的。 我把我遇到的一种情况总结如下: 首先,题目给的格式是2016-09-...

使用Python来开发微信功能

使用Python来开发微信功能

在移动社交时代,微信已经成为我们生活不可或缺的一部分。2017年的《微信数据报告》中显示:每天有380亿条消息从微信上发出,其中6亿条是语音消息,有350万个活跃的公众账号,并存在着8亿...

python画双y轴图像的示例代码

很多时候可能需要在一个图中画出多条函数图像,但是可能y轴的物理含义不一样,或是数值范围相差较大,此时就需要双y轴。 matplotlib和seaborn都可以画双y轴图像。 一个例子:...

pytorch实现用Resnet提取特征并保存为txt文件的方法

接触pytorch一天,发现pytorch上手的确比TensorFlow更快。可以更方便地实现用预训练的网络提特征。 以下是提取一张jpg图像的特征的程序: # -*- coding...