python概率计算器实例分析

作者:令狐不聪 时间:2021-01-16 05:34:52 

本文实例讲述了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,概率,计算
0
投稿

猜你喜欢

  • python中join与os.path.join()函数实例详解

    2023-08-23 19:20:51
  • oracle chm帮助文件下载

    2010-07-16 12:49:00
  • Python Pillow(PIL)库的用法详解

    2022-01-31 13:43:26
  • Sql Server基本函数

    2008-08-20 18:28:00
  • 43个应当避免的Web设计错误

    2007-09-30 12:57:00
  • opencv对多种颜色小球的形状及位置判断方式

    2022-12-18 22:47:50
  • 详解ASP图片上传保存到数据库功能

    2008-01-25 18:47:00
  • JavaScript性能优化--创建文档碎片

    2009-12-04 12:41:00
  • 用python实现一个简单计算器(完整DEMO)

    2023-11-10 11:03:48
  • 什么是Dynamic HTML(Dhtml)

    2010-03-16 12:40:00
  • python 自动去除空行的实例

    2021-04-12 08:46:04
  • 超半数中文网页一年内将“消失”

    2008-03-08 12:49:00
  • 捕捉并保存ASP运行错误的函数代码

    2012-11-30 20:24:43
  • 图文详解如何利用PyTorch实现图像识别

    2023-02-04 00:54:13
  • php自动获取字符串编码函数mb_detect_encoding

    2023-09-12 03:32:11
  • Python中url标签使用知识点总结

    2022-05-12 11:13:09
  • 基于Python编写一个宝石消消乐小游戏

    2021-10-25 05:46:06
  • 怎么用Python识别手势数字

    2021-11-14 16:35:35
  • PHP中isset()和unset()函数的用法小结

    2023-11-19 14:19:24
  • Python过滤序列元素的方法

    2023-07-05 17:53:37
  • asp之家 网络编程 m.aspxhome.com