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 解释器并重新进入,你做的任何定义(变量和方法)都会丢失。因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作为输入执...

详解Django将秒转换为xx天xx时xx分

Django将秒转换为xx天xx时xx分,具体代码如下所示: from django.utils.translation import ngettext_lazy as _n de...

python读取有密码的zip压缩文件实例

python读取有密码的zip压缩文件实例

今天试了一下用zipfile模块读取有密码的zip压缩文件。 今天用winrar 5.6将一个名字为1.xlsx的excel文件打包成1.zip压缩包。采用默认的压缩算法(没有勾选传统加...

python 实现从高分辨图像上抠取图像块

我就废话不多说了,直接上代码吧! #coding=utf-8 import cv2 import numpy as np import os # 程序实现功能: # 根据patch在...

在PyCharm中批量查找及替换的方法

在PyCharm中批量查找及替换的方法

选中需要操作的字符 Ctrl + R 替换 Ctrl + Shift + F 全局查找 Ctrl + Shift + R 全局替换 以上这篇在PyCharm中批量查找及替换的方法就是小...