I have written a program that simulates volleyball using rally scoring. In rally scoring, the team that wins the rally wins the point, regardless of which team is serving (for this reason my program ignores the issue of who’s serving). Games are played to a score of 30 and must be won by at least 2 points.
I actually have my program working. My problem is that I don’t understand why when I use a different version of the simOneGame function within my program, the program’s final output is so drastically different (and wrong).
The simOneGame function that generates good output is not commented out. The simOneGame function that generates bad output is commented out and has “#Why does this not work?” printed after the function definition.
An explanation of why the bad one doesn’t work would be greatly appreciated. Thanks.
# College volleyball simulation. The team that wins the rally is awarded # the point, even if they were not the serving team. games are played to a # score of 30. from random import random def printIntro(): print "This program simulates a game of volleyball between two" print 'teams called "A" and "B". The abilities of each team is' print "indicated by a probability (a number between 0 and 1) that" print "the team wins the point." def getInputs(): # Returns three simulation parameters probA, probB and n a = input("What is the prob. team A wins a rally? ") b = input("What is the prob. team B wins a rally? ") n = input("How many games to simulate? ") return a, b, n def simNGames(n, probA, probB): # Simulate n games and return winsA and winsB winsA = 0 winsB = 0 for i in range(n): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA = winsA + 1 else: winsB = winsB + 1 return winsA, winsB def simOneGame(probA, probB): # Simulates a single game of volleyball between two teams whose # abilities are represented by the probabiltiy or winning a rally. # Returns final scores for A and B. scoreA = 0 scoreB = 0 while not gameOver(scoreA, scoreB): if random()*probA > random()*probB: scoreA = scoreA + 1 else: scoreB = scoreB + 1 return scoreA, scoreB ##def simOneGame(probA, probB): #Why does this not work? ## # Simulates a single game of volleyball between two teams whose ## # abilities are represented by the probabiltiy or winning a rally. ## # Returns final scores for A and B. ## scoreA = 0 ## scoreB = 0 ## while not gameOver(scoreA, scoreB): ## if random()*probA > random()*probB: ## scoreA = scoreA + 1 ## elif random()*probA < random()*probB: ## scoreB = scoreB + 1 ## return scoreA, scoreB def gameOver(a, b): # a and b represent scores for a volleyball game. # Returns True if the game is over, False otherwise return (a >=30 and a - b >= 2) or (b >=30 and b - a >= 2) def printSummary(winsA, winsB): # prints a summary of wins for each team. n = winsA + winsB print "\nGames simulated:", n print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100) print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100) def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB) main()
Edited 3 Years Ago by Dani: Formatting fixed