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实现自动化办公学习笔记(CSV、word、Excel、PPT)

1、CSV (1)写csv文件 import csv def writecsv(path,data): with open(path, "w") as f: wri...

python移位运算的实现

密码算法程序设计实践选的SHA-1。 在写的过程中遇到一丢丢关于python移位的问题,记录一下。 SHA-1其中第一步需要填充消息。简单阐述一下sha1填充消息的过程: 如输入消息“...

深入浅析python定时杀进程

之前写了个python脚本用selenium+phantomjs爬新帖子,在循环拉取页面的过程中,phantomjs总是block住,使用WebDriverWait设置最长等待时间无效。...

Python的Bottle框架中返回静态文件和JSON对象的方法

Python的Bottle框架中返回静态文件和JSON对象的方法

代码如下: # -*- coding: utf-8 -*- #!/usr/bin/python # filename: todo.py # codedtime: 2014-8-28...

详解Python并发编程之从性能角度来初探并发编程

详解Python并发编程之从性能角度来初探并发编程

. 前言 作为进阶系列的一个分支「并发编程」,我觉得这是每个程序员都应该会的。 并发编程 这个系列,我准备了将近一个星期,从知识点梳理,到思考要举哪些例子才能更加让人容易吃透这些知识...