[Python] python概率计算器代码 →→→→→进入此内容的聊天室

来自 , 2020-12-06, 写在 Python, 查看 148 次.
URL http://www.code666.cn/view/bac4cbb6
  1. from random import randrange
  2. #randrange form random module
  3. def calc_prob(strengths):
  4.         """A function that receives an array of two numbers indicating the strength of each party and returns the winner"""
  5.         if strengths[1]>strengths[0]:#Bring the bigger number to the first position in the array
  6.         temp=strengths[0]
  7.         strengths[0]=strengths[1]
  8.         strengths[1]=temp      
  9.        
  10.     prob1=abs(strengths[0]-strengths[1])#The relative strength of the 2 parties
  11.    
  12.     prob2=randrange(0,100)#To calculate the luck that decides the outcome
  13.    
  14.     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
  15.    
  16.         return strengths[1]
  17.    
  18.     elif prob2 in range(33-prob1,66-prob1):#The middle condition
  19.    
  20.         return "Draw"
  21.    
  22.     else:
  23.    
  24.          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  
  25.        
  26. #Example
  27. calc_prob([50,75]);#Always has to be a list to allow exchange
  28. #Can be programmed in hundreds of better ways. Good luck!
  29. #//python/8469

回复 "python概率计算器代码"

这儿你可以回复上面这条便签

captcha