By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

i need help writing a program

P: 8
Design and implement a program that simulates a single volley ball game. In volleyball, only the serving team can score, and if the receiving team win the rally, they gain the serve. Games are played to a score of 15, but must be won by at least 2 points. If the scores are closer, the game can continue indefinitely. Your simulation should prompt the user for the service win probability of each player and loop until the one player wins. Print out the final scores.
can some one help me????
May 17 '07 #1
Share this Question
Share on Google+
4 Replies


dshimer
Expert 100+
P: 136
I could be totally wrong on this, and I don't speak for anyone, but I have noticed the following, which I offer in a humble attempt to be helpful.

When something looks (may or may not be) like a homework assignment posted nearly straight out of the book it tends to get very little helpful attention. There is so much knowledge and willingness to help here it would be a shame to miss out. Suggestions, do any of the following...

  • Form an overall strategy and ask for comments.
  • Break it into small tasks, write the code without regard to skill level and post for discussion. (use code tags)
  • Ask specific questions regarding specific problems that don't seem to offer ready solution.
  • Use terms and concepts that show evidence of having read the docs and learned some python.
May 17 '07 #2

P: 8
i cant get the score up to 15 but it's adding both player's to make 15
can some help

Expand|Select|Wrap|Line Numbers
  1. # volleyball.py
  2. # Simulation of a volleyball game. Illustrates use of random
  3. # numbers and functions to implement top-down design.
  4.  
  5. import random, random
  6.  
  7. def main():
  8.     Intro()
  9.     probA, probB, n = getInputs()
  10.     winsA, winsB = volleyball(n, probA, probB)
  11.     Summary(winsA, winsB)
  12.  
  13. def Intro():
  14.     # Prints an introduction to the program
  15.     print "This program simulates a game of volleyball between two"
  16.     print 'players called "player A" and "player B".  The abilities of each player is'
  17.     print "indicated by a probability (a number between 0 and 1) that"
  18.     print "the player wins the point when serving. Player A always"
  19.     print "has the first serve.\n"
  20.  
  21. def getInputs():
  22.     #This take input from user
  23.     a = input("What is the prob. player A wins a serve? ")
  24.     b = input("What is the prob. player B wins a serve? ")
  25.     n = 15
  26.     return a, b, n
  27.  
  28. def volleyball(n, probA, probB):
  29.     winsA = winsB = 0
  30.     for i in range(n):
  31.         scoreA, scoreB = volleyball_one(probA, probB)
  32.         if scoreA > scoreB:
  33.             winsA = winsA + 1
  34.         else:
  35.             winsB = winsB + 1
  36.     return winsA, winsB
  37.  
  38. def volleyball_one(probA, probB):
  39.     serving = "A"
  40.     scoreA = 0
  41.     scoreB = 0
  42.     while not gameOver(scoreA == 15, scoreB == 15):
  43.         if serving == "A":
  44.             if (random.random() < probA):
  45.                 scoreA = scoreA + 1
  46.             else:
  47.                 serving = "B"
  48.         else:
  49.             if (random.random() < probB):
  50.                 scoreB = scoreB + 1
  51.             else:
  52.                 serving = "A"
  53.         if (scoreA >= 15 or scoreB >= 15):
  54.             if (abs(scoreA - scoreB) >=2):
  55.                 break;
  56.     return scoreA, scoreB
  57.  
  58. def gameOver(a,b):
  59.     # a and b are scores for players in a volleyball game
  60.     # RETURNS true if game is over, false otherwise
  61.     return (a >= 15 and a - b >= 2) or (b >= 15 and b - a >= 2)
  62.  
  63. def Summary(winsA, winsB):
  64.     # Prints a summary of wins for each player.
  65.     n = winsA + winsB
  66.     print "\nGames simulated:", n
  67.     print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
  68.     print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100)
  69.  
  70. if __name__ == "__main__":
  71.     main()
May 18 '07 #3

bartonc
Expert 5K+
P: 6,596
Hi codie. My friend dshimer is right on point and since you seem to have copied your code directly from here, I'll have to suggest that other experts wait until you have made an honest effort at this problem to help with a solution.
May 18 '07 #4

P: 8
they give this code to the student's to start the assignment it in the python book from john zelle if you look in there is it a racquetball game and we are told to change the code to play as a volley ball game
and way thank and sorry for taking your time
May 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.