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

Creating VolleyBall game in python [solved]

P: 12
Q) Design and implement a volleyball simulation. 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.

Code given below plz correct it
Expand|Select|Wrap|Line Numbers
  1. #volleyball.py
  2.  
  3. def main ():
  4.     print Intro ()
  5.     probA, probB, n = getInputs ()
  6.     winsA, winsB = simNGames (n, probA, probB)
  7.     printSummary (winsA, winsB)
  8. def Intro ():
  9.     print "This program simulates a gsme of volleyball between"
  10.     print "two players called A and B."
  11.     print "player A always has the first serve"
  12. def getInputs ():
  13.     a = input ("Player A wins a serve")
  14.     b = input ("Player B wins a serve")
  15.     n = input ("How many games do you want to simulate?")
  16.     return a, b, n
  17. def simNGames(n, probA, probB) :
  18.     winsA= winsB = 0
  19.     for i in range(n):
  20.         scoreA, scoreB = simOneGame (probA, probB)
  21.         if scoreA > scoreB:
  22.             winsA =winsA + 1
  23.         else:
  24.             winsB = winB + 1
  25.         return winsA, winsB
  26. def simOneGame (probA, probB):
  27.     serving= "A"
  28.     scoreA = 0
  29.     scoreB = 0
  30.     while not gameOver (scoreA, scoreB):
  31.         if serving == "A":
  32.             if random() < probA:
  33.                 scoreA = scoreA + 1
  34.             else:
  35.                 serving = "B"
  36.         else:
  37.             if random() < probB:
  38.                     scoreB = scoreB + 1
  39.             else:
  40.                      serving = "A"
  41.  
  42.     return scoreA, scoreB
  43. def gameOver(a, b):
  44.     return a==15 or b==15
  45. def printSummary(winsA, winsB):
  46.     n= winsA + winsB
  47.     print "\nGames simulated", n
  48.     print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
  49.  
  50. if __name__ == '__main__':main()
Oct 22 '06 #1
Share this Question
Share on Google+
9 Replies


bartonc
Expert 5K+
P: 6,596
I won't format you code again!!! USE CODE TAGS!!!

That said, your program look pretty good, but you need to understand the difference between built-in python functions and library modules. For example, print is built-in

>>> print "hello"
hello

random is a module in the library that must be imported.

>>> random()
Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
random()
NameError: name 'random' is not defined

actually, you want one of the functions in that library so you can use
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2. for a in range(10):
  3.     print randint(0, 100) 
  4.  
55
12
55
9
70
61
51
53
24
79

In order to satisfy the last part
Expand|Select|Wrap|Line Numbers
  1. return (a >= 15 and a > (b + 2)) or (b >= 15 and b > (a+2))
  2.  
should be pretty close
Oct 22 '06 #2

kudos
Expert 100+
P: 126
here is some code to get you started...
(but do you really need to write a program to get the answer for this, or would it be simpler to just look up a statistics book????

-kudos

Expand|Select|Wrap|Line Numbers
  1. # playes ONE game
  2. # and some def around it to simulate more 
  3. # games..
  4.  
  5. import random, math
  6.  
  7. server = 0
  8. probA = .7
  9. teamA = 0
  10. teamB = 0
  11.  
  12. while(True):
  13.  if(server == 0):
  14.   print "Team A serves!"
  15.   if(random.random() < probA):
  16.    print "Team A wins the serve!"
  17.    teamA+=1
  18.   else:
  19.    server = 1
  20.  else:
  21.   print "Team B serves!"
  22.   if(random.random()>probA):
  23.    print "Team B wins the serve!"
  24.    teamB+=1
  25.   else:
  26.    server = 0
  27.  
  28.  if(teamA >= 15 or teamB >=15):
  29.   if(abs(teamA - teamB) >=2):
  30.    break;
  31.  
  32.  
  33. print "team A : " + str(teamA)
  34. print "team B : " + str(teamB)

Q) Design and implement a volleyball simulation. 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.

Code given below plz correct it
Expand|Select|Wrap|Line Numbers
  1. #volleyball.py
  2.  
  3. def main ():
  4.     print Intro ()
  5.     probA, probB, n = getInputs ()
  6.     winsA, winsB = simNGames (n, probA, probB)
  7.     printSummary (winsA, winsB)
  8. def Intro ():
  9.     print "This program simulates a gsme of volleyball between"
  10.     print "two players called A and B."
  11.     print "player A always has the first serve"
  12. def getInputs ():
  13.     a = input ("Player A wins a serve")
  14.     b = input ("Player B wins a serve")
  15.     n = input ("How many games do you want to simulate?")
  16.     return a, b, n
  17. def simNGames(n, probA, probB) :
  18.     winsA= winsB = 0
  19.     for i in range(n):
  20.         scoreA, scoreB = simOneGame (probA, probB)
  21.         if scoreA > scoreB:
  22.             winsA =winsA + 1
  23.         else:
  24.             winsB = winB + 1
  25.         return winsA, winsB
  26. def simOneGame (probA, probB):
  27.     serving= "A"
  28.     scoreA = 0
  29.     scoreB = 0
  30.     while not gameOver (scoreA, scoreB):
  31.         if serving == "A":
  32.             if random() < probA:
  33.                 scoreA = scoreA + 1
  34.             else:
  35.                 serving = "B"
  36.         else:
  37.             if random() < probB:
  38.                     scoreB = scoreB + 1
  39.             else:
  40.                      serving = "A"
  41.  
  42.     return scoreA, scoreB
  43. def gameOver(a, b):
  44.     return a==15 or b==15
  45. def printSummary(winsA, winsB):
  46.     n= winsA + winsB
  47.     print "\nGames simulated", n
  48.     print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
  49.  
  50. if __name__ == '__main__':main()
Oct 22 '06 #3

bartonc
Expert 5K+
P: 6,596
Looking up statistics in a book doesn't teach programming.

Looks like you learning! Keep it up! Have fun!

Barton
Oct 22 '06 #4

kudos
Expert 100+
P: 126
I only said that one could get the answer for this without doing a simulation.
Do I learn programming? what do you mean?


Looking up statistics in a book doesn't teach programming.

Looks like you learning! Keep it up! Have fun!

Barton
Oct 23 '06 #5

bartonc
Expert 5K+
P: 6,596
I only said that one could get the answer for this without doing a simulation.
Do I learn programming? what do you mean?
Sorry, I didn't notice that you had jumped in on this thread. I was writing to the original poster of this thread. Thanks for keeping an eye on this forum and contributing. We need all the help we can get!
Barton
Oct 23 '06 #6

P: 12
if(teamA >= 15 or teamB >=15):
if(abs(teamA - teamB) >=2):
break;

this code is not working, because the qestion says that the teams should be loss by only 2 points, but result goes other way. The program wins the game for team A but it wins by many points

i want to get something like if the winning team scored 15 and win the other team should atleast score 13. it will make 2 points different only.

any help?

thanx

Prince
Oct 27 '06 #7

bartonc
Expert 5K+
P: 6,596
if(teamA >= 15 or teamB >=15):
if(abs(teamA - teamB) >=2):
break;

this code is not working, because the qestion says that the teams should be loss by only 2 points, but result goes other way. The program wins the game for team A but it wins by many points

i want to get something like if the winning team scored 15 and win the other team should atleast score 13. it will make 2 points different only.

any help?

thanx

Prince
Prince, Please start using code tags - READ THE STICKY (first post) at the top of this forum - so your post looks like this:
Expand|Select|Wrap|Line Numbers
  1. if(teamA >= 15 or teamB >=15):
  2.     if(abs(teamA - teamB) >=2):
  3.         break;
  4.  
I ran the code that kudos gave you with probA = .5. It works for me.
Thanks for using code tags in your next post,
python forum moderator,
Barton
Oct 27 '06 #8

kudos
Expert 100+
P: 126
Didn't you say that one of the teams should win by atleast two points? Also, isn't it a bit odd (as a game) the constraint that the winning team should have 15 points, and the loosing team should have no less than 13 points?

-kudos



if(teamA >= 15 or teamB >=15):
if(abs(teamA - teamB) >=2):
break;

this code is not working, because the qestion says that the teams should be loss by only 2 points, but result goes other way. The program wins the game for team A but it wins by many points

i want to get something like if the winning team scored 15 and win the other team should atleast score 13. it will make 2 points different only.

any help?

thanx

Prince
Oct 27 '06 #9

P: 12
yes that's right but i dunno some how its not working correctly, but anyway guys thanx for your help and time. I really appreciate you guys help

i got a better idea now and will try to solve it out

thanx a lot
Oct 28 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.