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单线程文件传输的实例(C/S)

python单线程文件传输的实例(C/S)

客户端代码: #-*-encoding:utf-8-*- import socket import os import sys import math import time...

Python如何生成树形图案

Python如何生成树形图案

本文实例为大家分享了Python生成树形图案的具体代码,供大家参考,具体内容如下 先看一下效果,见下图。 上面这颗大树是使用Python + Tkinter绘制的,主要原理为使用分形画...

在Python的Flask框架中使用日期和时间的教程

在Python的Flask框架中使用日期和时间的教程

 时间戳的问题 我们的微博应用的一个忽略了很久的问题就是日间和日期的显示。 直到现在,我们在我们的User和Post对象中使用Python它自己的方式来渲染时间对象,但这并不是...

小米5s微信跳一跳小程序python源码

本文实例为大家分享了微信跳一跳小程序python源码,供大家参考,具体内容如下 微信跳一跳小程序小米5s源码python,搭建环境后亲测可用。 # coding: utf-8 imp...

Python实现监控键盘鼠标操作示例【基于pyHook与pythoncom模块】

Python实现监控键盘鼠标操作示例【基于pyHook与pythoncom模块】

本文实例讲述了Python实现监控键盘鼠标操作。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- import pythoncom import py...