Please support our Python advertiser: Programming Forums
Views: 1060 | Replies: 2
![]() |
•
•
Join Date: Mar 2007
Location: Australia
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hi all,
I've been working on a program that simulates a racquetball match. The problem I am having is getting the match to end when one player is the clear winner of 'n' games. I've tried adding modules, altering loops, adding variables, etc.
Is anyone able to help by pointing me in the right direction? I've spent many days and sleepless nights on this and have just hit a brick wall. I don't want it to get the better of me.
:-|
Here is my code so far:
I've been working on a program that simulates a racquetball match. The problem I am having is getting the match to end when one player is the clear winner of 'n' games. I've tried adding modules, altering loops, adding variables, etc.
Is anyone able to help by pointing me in the right direction? I've spent many days and sleepless nights on this and have just hit a brick wall. I don't want it to get the better of me.
:-|
Here is my code so far:
python Syntax (Toggle Plain Text)
# rballmatch.py # Simulation of a racquetball game. . from random import random def printIntro(): # Prints an introduction to the program print "This program simulates a game of racquetball between two" print 'players called "A" and "B". The abilities of each player is' print "indicated by a probability (a number between 0 and 1) that" print "the player wins the point when serving. Player A always" print "has the first serve.\n" def getInputs(): # Returns probA, probB, number of games to simulate a = input("What is the prob. player A wins a serve? ") b = input("What is the prob. player B wins a serve? ") n = input("How many games to simulate? ") return a, b, n def simNGames(n, probA, probB): # Simulates n games of racquetball between players whose # abilities are represented by the probability of winning a serve. # Returns number of wins for A and B winsA = winsB = 0 if (winsA) or (winsB) = ((n / 2) + 1: break else: 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 racquetball between two players whose # abilities are represented by the probability of winning a serve. # Returns final scores for A and B serving = "A" scoreA = 0 scoreB = 0 while not gameOver(scoreA, scoreB): if serving == "A": if random() < probA: scoreA = scoreA + 1 else: serving = "B" else: if random() < probB: scoreB = scoreB + 1 else: serving = "A" return scoreA, scoreB def gameOver(a,b): # a and b are scores for players in a racquetball game # Returns true if game is over, false otherwise return a == 15 or b == 15 and ((a - b) > 2 ) or ((a - b) < -2) def printSummary(winsA, winsB): # Prints a summary of wins for each player. 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) if __name__ == "__main__": main()
One of your problems is right here:
You have to change the if statement logic a bit, and you are using break when you are clearly not in a loop.
if (winsA) or (winsB) == ((n / 2) + 1):
break No one died when Clinton lied.
•
•
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
python Syntax (Toggle Plain Text)
def gameOver(a,b): # a and b are scores for players in a racquetball game # RETURNS true if game is over, false otherwise return a == 15 or b == 15 and abs(a-b)>= 2
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode