Guest User

Untitled

a guest
Feb 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. # U09_Ex04_VolleyballSimulation.py
  2. #
  3. # Author: Cole Hudson
  4. # Course: Coding for OOP
  5. # Section: A3
  6. # Date: 9 Feb 2018
  7. # IDE: PyCharm Community Edition
  8. #
  9. # Assignment Info
  10. # Exercise 4
  11. # Source: Python Programming
  12. # Chapter: 9
  13. #
  14. # Program Description
  15. # Simulates a game of volleyball for two teams
  16. #
  17. # Algorithm (pseudocode)
  18. # Print an introduction

  19. # Get the inputs: a, b, nameA, nameB
  20. # 
Write code in a new function for simulating game using if statements and the use of random
  21. # Write code in the same function specifically for determining if the game has been won by 2 or more points
  22. # Write code in a new function for determining if the game is over or not, game must be won b
  23. # Print Results
  24.  
  25. from random import random
  26.  
  27. def main():
  28. printIntro()
  29. a, b = getValues()
  30. scoreA, scoreB = simGame(a, b)
  31. printSummary(scoreA, scoreB)
  32.  
  33. def printIntro():
  34. print("This is a program that simulates a game of volleyball for two teams. You will be inputting some values in.")
  35.  
  36. def getValues():
  37. a = float(input("What is the probability that team A wins a rally? (enter as decimal ≤ 1) "))
  38. b = float(input("What is the probability that team B wins a rally? (enter as decimal ≤ 1) "))
  39. return a, b
  40.  
  41. def simGame(a, b):
  42. scoreA = 0; scoreB = 0
  43. server = 'A'
  44. while not gameOver(scoreA, scoreB):
  45. prob = random()
  46. if server == 'A':
  47. if prob < a:
  48. scoreA += 1
  49. else:
  50. server = 'B'
  51. else:
  52. if prob < b:
  53. scoreB += 1
  54. else:
  55. scoreA += 1
  56. return scoreA, scoreB
  57.  
  58. def gameOver(a, b):
  59. return (a >= 25 or b >= 25) and abs(a - b) >= 2
  60.  
  61.  
  62. def printSummary(scoreA, scoreB):
  63. print("\nYour results are:")
  64. print("Rounds won for Team A: {}".format(scoreA))
  65. print("Rounds won for Team B {}".format(scoreB))
  66. if scoreA > scoreB:
  67. print("Team A wins!")
  68. else:
  69. print("Team B wins!")
  70.  
  71. if __name__ == '__main__':
  72. main()
Add Comment
Please, Sign In to add comment