Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # U09_Ex04_VolleyballSimulation.py
- #
- # Author: Cole Hudson
- # Course: Coding for OOP
- # Section: A3
- # Date: 9 Feb 2018
- # IDE: PyCharm Community Edition
- #
- # Assignment Info
- # Exercise 4
- # Source: Python Programming
- # Chapter: 9
- #
- # Program Description
- # Simulates a game of volleyball for two teams
- #
- # Algorithm (pseudocode)
- # Print an introduction
- # Get the inputs: a, b, nameA, nameB
- # Write code in a new function for simulating game using if statements and the use of random
- # Write code in the same function specifically for determining if the game has been won by 2 or more points
- # Write code in a new function for determining if the game is over or not, game must be won b
- # Print Results
- from random import random
- def main():
- printIntro()
- a, b = getValues()
- scoreA, scoreB = simGame(a, b)
- printSummary(scoreA, scoreB)
- def printIntro():
- print("This is a program that simulates a game of volleyball for two teams. You will be inputting some values in.")
- def getValues():
- a = float(input("What is the probability that team A wins a rally? (enter as decimal ≤ 1) "))
- b = float(input("What is the probability that team B wins a rally? (enter as decimal ≤ 1) "))
- return a, b
- def simGame(a, b):
- scoreA = 0; scoreB = 0
- server = 'A'
- while not gameOver(scoreA, scoreB):
- prob = random()
- if server == 'A':
- if prob < a:
- scoreA += 1
- else:
- server = 'B'
- else:
- if prob < b:
- scoreB += 1
- else:
- scoreA += 1
- return scoreA, scoreB
- def gameOver(a, b):
- return (a >= 25 or b >= 25) and abs(a - b) >= 2
- def printSummary(scoreA, scoreB):
- print("\nYour results are:")
- print("Rounds won for Team A: {}".format(scoreA))
- print("Rounds won for Team B {}".format(scoreB))
- if scoreA > scoreB:
- print("Team A wins!")
- else:
- print("Team B wins!")
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment