1 # badminton match prediction 2 from Random Import Random . 3 DEF printTips (): . 4 Print ( " I Student ID: 2,019,310,143,143 " ) . 5 Print ( " This program simulates two singles tennis match, the second round of qualifying game " ) 6 print ( " Each round takes seven innings and four wins " ) 7 print ( "The program requires the ability value of each contestant (expressed as a decimal between 0 and 1) " ) 8 def getInputs (): 9 a = eval (input ( " Please enter player A's ability value (0-1): ")) 10 b = eval (input ( " Please enter player B's ability value (0-1): " )) 11 c = eval (input ( " please enter player C's ability value (0-1): " )) 12 d = eval (input ( " Please enter player D's ability value (0-1): " )) 13 return a, b, c, d 14 def ModelNGames (powerA, powerB): # 4 wins in 7 games 15 winsA, winsB = 0, 0 16 for i in range (7 ): 17 scoreA, scoreB = ModelOneGame (powerA, powerB) 18 if scoreA>scoreB: . 19 winsA + =. 1 20 is the else : 21 is winsB + =. 1 22 is return winsA, winsB 23 is DEF gameOver (A, B): # determines race victory innings 24 IF A> 10 or B> 10 : 25 IF ABS ( ab &)>. 1 : 26 is return True 27 the else : 28 return False 29 DEF Judge (winsA, winsB): # determines a game victory 30 IF winsA> =. 4 or winsB> =. 4: 31 return True 32 else: 33 return False 34 def ModelOneGame(powerA, powerB):#一局比赛 35 scoreA, scoreB = 0, 0 36 serving = "A" 37 while not gameOver(scoreA, scoreB): 38 if serving == "A": 39 if random() < powerA: 40 scoreA += 1 41 else: 42 = Serving " B " 43 is the else : 44 is IF Random () < Powerb: 45 scoreB. 1 = + 46 is the else : 47 Serving = " A " 48 return scoreA, scoreB 49 DEF printResult1 (winsA, winsB): # The first round first Field result 50 if winsA> winsB: 51 print ( "The score of A and B is {}: {}, player A wins, the promotion is successful " .format (winsA, winsB)) 52 else : 53 Print ( " A} and {B score is: {}, B players win promotion success " .format (winsA, winsB)) 54 is DEF printResult2 (winsA, winsB): # result of the first round of the second of the 55 IF winsA > winsB: 56 print ( "The score of C and D is {}: {}, player C wins and advances successfully " .format (winsA, winsB)) 57 else : 58 print ( "The score of C and D is {}: {} , D players win promotion success " .format (winsA, winsB)) 59 DEF printResult3 (WINSE, winsF, winner): # outputs two results 60 IF WINSE> winsF: 61 is print ( "The score is {}: {}, the player {} won the championship " .format (winsE, winsF, winner)) 62 else : 63 print ( "The score is {}: {}, the player {} won the championship " .format (winsE, winsF, winner)) 64 def main (): 65 printTips () 66 powerA, powerB, powerC, powerD = getInputs () 67 winsA, winsB = ModelNGames (powerA, powerB) 68 printResult1 (winsA, winsB) 69 winsC , winsD = ModelNGames (powerC, powerD) 70 printResult2 (winsC, winsD) 71 E =max (winsA, winsB) 72 F. = max (winsC, winsD) 73 is powerE, powerF = 0,0 74 IF E == winsA and F. == winsC: # ability to find two values of cut 75 powerE = powerA 76 powerF = POWERC 77 elif E == winsB and F. == winsC: 78 powerE = Powerb 79 powerF = POWERC 80 elif E == winsA and F. == winsD: 81 powerE = powerA 82 powerF = powerD 83 elif E == winsB and F == winsD: 84 powerE = powerB 85 powerF = powerD 86 winsE, winsF = ModelNGames (powerE, powerF) 87 if winsE> winsF: # 找到 冠军 88 if powerE == powerA: 89 winner = ' A ' 90 else : 91 winner = ' B ' 92 else : 93 if powerF ==powerC: 94 winner='C' 95 else : 96 winner='D' 97 printResult3(winsE,winsF,winner) 98 main()
Note: This code is only suitable for the promotion of two singles (A and B, C and D) doubles, you can judge the champion.