Advertisement
Nybble

Untitled

Jun 20th, 2025
384
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | Source Code | 0 0
  1. # volleyball.py
  2.  
  3. from random import random
  4.  
  5. def printIntro():
  6. # Add code to complete the introduction, briefly explaining the two types
  7. # of game simulated. (See racquetball example, from pages 348-350.)
  8.     print("This program simulates volleyball games between two")
  9.  
  10. def getInputs():
  11.     a = float(input("What is the prob. player A wins a serve? "))
  12.     b = float(input("What is the prob. player B wins a serve? "))
  13.     n = int(input("How many games to simulate? "))
  14.     return a, b, n
  15.  
  16. def normalGameOver(scoreA, scoreB):
  17.     return scoreA == 25 or scoreB == 25
  18.  
  19. def rallyGameOver(scoreA, scoreB):
  20. # Replace this with code to correctly determine whether the game is over;
  21. # return True if it is, False if it isn't.
  22.     return (scoreA >= 15 and scoreA - scoreB >= 2) or (scoreB >= 15 and scoreB - scoreA >= 2)
  23.  
  24. def simOneGame(probA, probB, scoring):
  25.  
  26.     #a different shorter way of achieving same result would be?:
  27.     """
  28.    if random()*probA > random()*probB:
  29.        scoreA = scoreA + 1 # scoreA is higher than scoreB
  30.    else:
  31.        scoreB = scoreB + 1 # scoreB is the higher value
  32.    """
  33.     serving = "A"
  34.     scoreA = 0
  35.     scoreB = 0
  36.     if scoring == "normal":
  37.         while not normalGameOver(scoreA, scoreB):
  38.             if serving == "A":
  39.                 if random() < probA:
  40.                     scoreA = scoreA + 1
  41.                 else:
  42.                     serving = "B"
  43.             else: # B serving
  44.                 if random() < probB:
  45.                     scoreB = scoreB + 1
  46.                 else:
  47.                     serving = "A"
  48.     else: # if scoring == "rally"
  49.         while not rallyGameOver(scoreA, scoreB):
  50.             if serving == "A":
  51.                 if random() < probA:
  52.                     scoreA = scoreA + 1
  53.                 else:
  54.                     serving = "B"
  55.             else: # B serving
  56.                 if random() < probB:
  57.                     scoreB = scoreB + 1
  58.                 else:
  59.                     serving = "A"
  60.     return scoreA, scoreB
  61.  
  62. def simNGames(n, probA, probB, scoring):
  63.     winsA = winsB = 0
  64.     for i in range(n):
  65.         scoreA, scoreB = simOneGame(probA, probB, scoring)
  66.         if scoreA > scoreB:
  67.             winsA = winsA + 1
  68.         else: # (Tie not possible.)
  69.             winsB = winsB + 1
  70.     return winsA, winsB
  71.  
  72. def printSummary(winsA, winsB):
  73.     n = winsA + winsB
  74.     print("\n Games simulated:", n)
  75.     print(f" Wins for A: {winsA} ({(winsA / n):0.1%})")
  76.     print(f" Wins for B: {winsB} ({(winsB / n):0.1%})")
  77.  
  78. def main():
  79.  
  80.     printIntro()
  81.     probA, probB, n = getInputs()
  82.     print()
  83.     print("Normal Scoring".center(30))
  84.     print("-" * 30)
  85.  
  86.     winsA, winsB = simNGames(n, probA, probB, "normal")
  87.     printSummary(winsA, winsB)
  88.     print()
  89.     print("Rally Scoring".center(30))
  90.     print("-" * 30)
  91.  
  92.     winsA, winsB = simNGames(n, probA, probB, "rally")
  93.     printSummary(winsA, winsB)
  94.  
  95. if __name__ == "__main__":
  96.     main()
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement