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实现交叉验证法与留出法

详解python实现交叉验证法与留出法

在机器学习中,我们经常在训练集上训练模型,在测试集上测试模型。最终的目标是希望我们的模型在测试集上有最好的表现。 但是,我们往往只有一个包含m个观测的数据集D,我们既要用它进行训练,又要...

修改Pandas的行或列的名字(重命名)

pandas.DataFrame.rename 使用函数: DataFrame.rename(mapper=None, index=None, colum...

python3音乐播放器简单实现代码

本文实例为大家分享了python3音乐播放器的关键代码,供大家参考,具体内容如下 from tkinter import * from traceback import * from...

python读取中文txt文本的方法

对于python2.7 字符串在Python2.7内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码成unicode,...

python根据日期返回星期几的方法

本文实例讲述了python根据日期返回星期几的方法。分享给大家供大家参考。具体如下: 这个函数给定日期,输出星期几,至于0是星期一还是星期天,这和时区有关,反正我这里是0表示星期一...