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 -
#volleyball.py
-
-
def main ():
-
print Intro ()
-
probA, probB, n = getInputs ()
-
winsA, winsB = simNGames (n, probA, probB)
-
printSummary (winsA, winsB)
-
def Intro ():
-
print "This program simulates a gsme of volleyball between"
-
print "two players called A and B."
-
print "player A always has the first serve"
-
def getInputs ():
-
a = input ("Player A wins a serve")
-
b = input ("Player B wins a serve")
-
n = input ("How many games do you want to simulate?")
-
return a, b, n
-
def simNGames(n, probA, probB) :
-
winsA= winsB = 0
-
for i in range(n):
-
scoreA, scoreB = simOneGame (probA, probB)
-
if scoreA > scoreB:
-
winsA =winsA + 1
-
else:
-
winsB = winB + 1
-
return winsA, winsB
-
def simOneGame (probA, probB):
-
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):
-
return a==15 or b==15
-
def printSummary(winsA, winsB):
-
n= winsA + winsB
-
print "\nGames simulated", n
-
print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
-
-
if __name__ == '__main__':main()
9 9585 bartonc 6,596
Recognized Expert Expert
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 -
from random import randint
-
for a in range(10):
-
print randint(0, 100)
-
55
12
55
9
70
61
51
53
24
79
In order to satisfy the last part -
return (a >= 15 and a > (b + 2)) or (b >= 15 and b > (a+2))
-
should be pretty close
kudos 127
Recognized Expert New Member
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 - # playes ONE game
-
# and some def around it to simulate more
-
# games..
-
-
import random, math
-
-
server = 0
-
probA = .7
-
teamA = 0
-
teamB = 0
-
-
while(True):
-
if(server == 0):
-
print "Team A serves!"
-
if(random.random() < probA):
-
print "Team A wins the serve!"
-
teamA+=1
-
else:
-
server = 1
-
else:
-
print "Team B serves!"
-
if(random.random()>probA):
-
print "Team B wins the serve!"
-
teamB+=1
-
else:
-
server = 0
-
-
if(teamA >= 15 or teamB >=15):
-
if(abs(teamA - teamB) >=2):
-
break;
-
-
-
print "team A : " + str(teamA)
-
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 -
#volleyball.py
-
-
def main ():
-
print Intro ()
-
probA, probB, n = getInputs ()
-
winsA, winsB = simNGames (n, probA, probB)
-
printSummary (winsA, winsB)
-
def Intro ():
-
print "This program simulates a gsme of volleyball between"
-
print "two players called A and B."
-
print "player A always has the first serve"
-
def getInputs ():
-
a = input ("Player A wins a serve")
-
b = input ("Player B wins a serve")
-
n = input ("How many games do you want to simulate?")
-
return a, b, n
-
def simNGames(n, probA, probB) :
-
winsA= winsB = 0
-
for i in range(n):
-
scoreA, scoreB = simOneGame (probA, probB)
-
if scoreA > scoreB:
-
winsA =winsA + 1
-
else:
-
winsB = winB + 1
-
return winsA, winsB
-
def simOneGame (probA, probB):
-
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):
-
return a==15 or b==15
-
def printSummary(winsA, winsB):
-
n= winsA + winsB
-
print "\nGames simulated", n
-
print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
-
-
if __name__ == '__main__':main()
bartonc 6,596
Recognized Expert Expert
Looking up statistics in a book doesn't teach programming.
Looks like you learning! Keep it up! Have fun!
Barton
kudos 127
Recognized Expert New Member
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
bartonc 6,596
Recognized Expert Expert
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
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
bartonc 6,596
Recognized Expert Expert
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: -
if(teamA >= 15 or teamB >=15):
-
if(abs(teamA - teamB) >=2):
-
break;
-
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
kudos 127
Recognized Expert New Member
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: O'Neal Computer Programmer |
last post by:
I was reading here: http://groups.google.com/groups?q=elemental+group:comp.lang.python.*&hl=en&lr=&ie=UTF-8&group=comp.lang.python.*&selm=mailman.1044572235.32593.python-list%40python.org&rnum=3...
|
by: BlackHawke |
last post by:
Hello!
My name is Nick Soutter, I am the owner of a small game company in San
Diego, CA called Aepox Games (www.aepoxgames.net).
Our first product, Andromeda Online...
|
by: Sam |
last post by:
Hey all,
I want to create a computerised version of this game, though I'm not
really sure how to go about it. For those who don't know how the game
works, here's my attempt at a brief...
|
by: Moosebumps |
last post by:
When I have time, I am planning to evaluate Python for console game
development (on Playstation 2, GameCube, and Xbox). Does anyone have any
experience with this?
Pretty much the only resource...
|
by: Edvard Majakari |
last post by:
Greetings, fellow Pythonistas!
I'm about to create three modules. As an avid TDD fan I'd like to create
typical 'use-cases' for each of these modules. One of them is rather large,
and I wondered...
| |
by: kanedatakeshi |
last post by:
Hi folks!
The problem is the following:
I use Python 2.1 embedded in a C++ game engine. Some of the engines
complexer objects (AI, game logic, etc.) use python classes that act as
an...
|
by: Michael Goettsche |
last post by:
Hi there,
for a project in our computer science lessons at school we decided to write a
client/server based battleship like game .
I know this game could be written without a server, but the...
|
by: Jerry Fleming |
last post by:
Hi,
I have wrote a game with python curses. The problem is that I want to
confirm before quitting, while my implementation doesn't seem to work.
Anyone can help me?
#!/usr/bin/python
#
#...
|
by: Michael Lubker |
last post by:
Any people that use Python as the predominant language for their game
development here?
~Michael
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |