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 9562
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
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()
Looking up statistics in a book doesn't teach programming.
Looks like you learning! Keep it up! Have fun!
Barton
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
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
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
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |