
Simulates the tennis game
Program Plan:
- Import the header file.
- Define the “main” method.
- Call the “printIntro ()” method
- Call the “getInputs ()” method.
- Call the “simNMatches ()” method.
- Call the “printSummary ()” method.
- Define the “printIntro()” method.
- Print the intro statements.
- Define the “getInputs()” method.
- Get the player A possible for win from the user.
- Calculate the “probA” and “probB” values.
- Get how many games to simulate from the user.
- Define the “simNMatches()” method.
- Set the values
- Iterate “i” until it reaches “n” value
- Call the methods
- Check “matchA” is greater than “matchB”
- Increment the “winsA” value
- Otherwise, increment the “winsB” value.
- Return the values.
- Define the “simOneMatch ()” method
- Set the values
- Check the condition.
- Call the “simOneGame ()” method.
- Check “setA” is greater than “setB”
- Increment the “matchA” value
- Otherwise, increment the “matchB” value
- Return the results
- Define the “matchOver ()” method
- Check “a” or “b” is greater than 3
- Return true
-
- Otherwise, return false.
-
- Return true
- Check “a” or “b” is greater than 3
- Define the “simOneSet ()” method
- Call the method
- Set the values
- Check the condition.
- Check “scoreA” is greater than “scoreB”
- Increment the “setA” value
- Otherwise, increment the “setB” value
-
- Return the results
-
- Check “scoreA” is greater than “scoreB”
- Define the “setOver ()” method
- Check “a” or “b” is equal to 7
- Return true
-
- Check “a” or “b” is greater than or equal to 6
-
- Check “a-b” is greater than or equal to 2
- Return true
- Otherwise, return false
- Check “a-b” is greater than or equal to 2
- Otherwise, return false.
-
- Return true
- Check “a” or “b” is equal to 7
- Define the “simOneGame ()” method
- Set the values
- Check the condition
- Check “random ()” is less than “probA”
- Increment the “scoreA” value
-
- Otherwise, increment the “scoreB” value
- Return the results.
-
- Increment the “scoreA” value
- Check “random ()” is less than “probA”
- Define the “gameOver ()” method
- Check “a” or “b” is greater than or equal to 4
- Check “a-b” is greater than or equal to 2
- Return true
-
- Otherwise, return false
- Otherwise, return false.
-
- Return true
- Check “a-b” is greater than or equal to 2
- Check “a” or “b” is greater than or equal to 4
- Define the “printSummary ()” method
- Display the results.
- Call the main method.

The program is to simulate a game of tennis.
Explanation of Solution
Program:
#import the header file
from random import random
#definition of main method
def main():
#call the method
printIntro()
#call the method and store it in the variables
probA, probB, n = getInputs()
#call the method and store it in the variables
winsA, winsB = simNMatches(n, probA, probB)
#call the method
printSummary(winsA, winsB, n)
#definition of "printIntro" method
def printIntro():
#display the statements
print("This program simulates a series of tennis matches between player")
print('"A" and player "B". The abilities of each player are')
print("represented by percentage chance of winning a volley. The")
print("percentages add up to 100.")
print()
print("Game")
print("As in real tennis, each game is played through 4 points")
print("(Love, 15, 30, 40, game) where the player must win by two.")
print("Players can score on either serve.")
print()
print("Set")
print("A set is won when a player reaches 6 victorious games, and has a")
print("lead of two. If for example, sets reach 6-5, the players will play")
print("another round. If the score reaches 6-6, there will be a")
print("tiebreaking game.")
print()
print("Match")
print("A Match is won when a player reaches his/her 3rd victorious set.")
print("No winning by two, no tie-breaker, for the purposes of this simulation")
print()
#definition of "getInputs" method
def getInputs():
#get the player A wins a serve
probA = eval(input("What is the percent prob. player A wins a volley? "))
#calculate the values
probA = probA / 100
probB = 1 - probA
#get the input from the user
n = eval(input("How many games to simulate? "))
return probA, probB, n
#definition of "simNMatches" method
def simNMatches(n, probA, probB):
#set the values
winsA = winsB = 0
#iterate until "n" value
for i in range(n):
#call the method and store it in the variables
matchA, matchB = simOneMatch(probA, probB)
#check "matchA" is greater than "matchB"
if matchA > matchB:
#increment the value
winsA = winsA + 1
#otherwise
else:
#increment the value
winsB = winsB + 1
#return the results
return winsA, winsB
#definition of "simOneMatch" method
def simOneMatch(probA, probB):
#set the values
matchA = matchB = 0
#check the condition
while not matchOver(matchA, matchB):
#call the method and store it in the variables
setA, setB = simOneSet(probA, probB)
#check "setA" is greater than "setB"
if setA > setB:
#increment the value
matchA = matchA + 1
#otherwise
else:
#increment the value
matchB = matchB + 1
#return the results
return matchA, matchB
#definition of "matchOver" method
def matchOver(a, b):
#check "a" or "b" is greater than 3
if a > 3 or b > 3:
#return the result
return True
else:
#return the result
return False
#definition of "simOneSet" method
def simOneSet(probA, probB):
#call the method and store it in the variables
scoreA, scoreB = simOneGame(probA, probB)
#set the values
setA = setB = 0
#check the condition
while not setOver(setA, setB):
#check "scoreA" is greater than "scoreB"
if scoreA > scoreB:
#increment the value
setA = setA + 1
else:
#increment the value
setB = setB + 1
#return the results
return setA, setB
#definition of "setOver" method
def setOver(a, b):
#check "a" and "b" is equal to 7
if a == 7 or b == 7:
#return the result
return True
#check "a" or "b" is greater than or equal to 6
elif a >= 6 or b >= 6:
#check "a - b" is greater than or equal to 2
if abs(a-b) >=2:
#return the result
return True
else:
#return the result
return False
else:
#return the result
return False
#definition of "simOneGame" method
def simOneGame(probA, probB):
#set the values
scoreA = scoreB = 0
#check the condition
while not gameOver(scoreA, scoreB):
#check "random()" is less than "probA"
if random() < probA:
#increment the value
scoreA = scoreA + 1
else:
#increment the value
scoreB = scoreB + 1
#return the result
return scoreA, scoreB
#definition of "gameOver" method
def gameOver(a, b):
#check "a" or "b" is greater than or equal to 4
if a >= 4 or b >= 4:
#check "a - b" is greater than or equal to 2
if abs(a-b) >=2:
#return the result
return True
else:
#return the result
return False
else:
#return the result
return False
#definition of "printSummary" method
def printSummary(winsA, winsB, n):
#display the results
print("\nGames simulated: ", n)
print("Wins for A: {0} ({1:0.1%})".format(winsA, winsA/n))
print("Wins for B: {0} ({1:0.1%})".format(winsB, winsB/n))
#call the main method
if __name__ == '__main__': main()
Output:
This program simulates a series of tennis matches between player
"A" and player "B". The abilities of each player are
represented by percentage chance of winning a volley. The
percentages add up to 100.
Game
As in real tennis, each game is played through 4 points
(Love, 15, 30, 40, game) where the player must win by two.
Players can score on either serve.
Set
A set is won when a player reaches 6 victorious games, and has a
lead of two. If for example, sets reach 6-5, the players will play
another round. If the score reaches 6-6, there will be a
tiebreaking game.
Match
A Match is won when a player reaches his/her 3rd victorious set.
No winning by two, no tie-breaker, for the purposes of this simulation
What is the percent prob. player A wins a volley? 50
How many games to simulate? 10
Games simulated: 10
Wins for A: 7 (70.0%)
Wins for B: 3 (30.0%)
Want to see more full solutions like this?
Chapter 9 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- What are the four types of feasibility? what is the issues addressed by each feasibility component.arrow_forwardI would like to get ab example of a situation where Agile Methods might be preferable versus the traditional SDLC? What are the characteristics of this situation that give Agile Methods an advantage?arrow_forwardWhat is a functional decomposition diagram? what is a good example of a high level task being broken down into tasks in at least two lower levels (three levels in all).arrow_forward
- What are the advantages to using a Sytems Analysis and Design model like the SDLC vs. other approaches?arrow_forward3. Problem Description: Define the Circle2D class that contains: Two double data fields named x and y that specify the center of the circle with get methods. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea() that returns the area of the circle. • A method getPerimeter() that returns the perimeter of the circle. • • • A method contains(double x, double y) that returns true if the specified point (x, y) is inside this circle. See Figure (a). A method contains(Circle2D circle) that returns true if the specified circle is inside this circle. See Figure (b). A method overlaps (Circle2D circle) that returns true if the specified circle overlaps with this circle. See the figure below. р O со (a) (b) (c)< Figure (a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle overlaps another…arrow_forward1. Explain in detail with examples each of the following fundamental security design principles: economy of mechanism, fail-safe default, complete mediation, open design, separation of privilege, least privilege, least common mechanism, psychological acceptability, isolation, encapsulation, modularity, layering, and least astonishment.arrow_forward
- Security in general means the protection of an asset. In the context of computer and network security, explore and explain what assets must be protected within an online university. What the threats are to the security of these assets, and what countermeasures are available to mitigate and protect the organization from such threats. For each of the assets you identify, assign an impact level (low, moderate, or high) for the loss of confidentiality, availability, and integrity. Justify your answers.arrow_forwardPlease include comments and docs comments on the program. The two other classes are Attraction and Entertainment.arrow_forwardObject-Oriented Programming In this separate files. ent, you'll need to build and run a small Zoo in Lennoxville. All classes must be created in Animal (5) First, start by building a class that describes an Animal at a Zoo. It should have one private instance variable for the name of the animal, and one for its hunger status (fed or hungry). Add methods for setting and getting the hunger satus variable, along with a getter for the name. Consider how these should be named for code clarity. For instance, using a method called hungry () to make the animal hungry could be used as a setter for the hunger field. The same logic could be applied to when it's being fed: public void feed () { this.fed = true; Furthermore, the getter for the fed variable could be named is Fed as it is more descriptive about what it answers when compared to get Fed. Keep this technique in mind for future class designs. Zoo (10) Now we have the animals designed and ready for building a little Zoo! Build a class…arrow_forward
- 1.[30 pts] Answer the following questions: a. [10 pts] Write a Boolean equation in sum-of-products canonical form for the truth table shown below: A B C Y 0 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 a. [10 pts] Minimize the Boolean equation you obtained in (a). b. [10 pts] Implement, using Logisim, the simplified logic circuit. Include an image of the circuit in your report. 2. [20 pts] Student A B will enjoy his picnic on sunny days that have no ants. He will also enjoy his picnic any day he sees a hummingbird, as well as on days where there are ants and ladybugs. a. Write a Boolean equation for his enjoyment (E) in terms of sun (S), ants (A), hummingbirds (H), and ladybugs (L). b. Implement in Logisim, the logic circuit of E function. Use the Circuit Analysis tool in Logisim to view the expression, include an image of the expression generated by Logisim in your report. 3.[20 pts] Find the minimum equivalent circuit for the one shown below (show your work): DAB C…arrow_forwardWhen using functions in python, it allows us tto create procedural abstractioons in our programs. What are 5 major benefits of using a procedural abstraction in python?arrow_forwardFind the error, assume data is a string and all variables have been declared. for ch in data: if ch.isupper: num_upper = num_upper + 1 if ch.islower: num_lower = num_lower + 1 if ch.isdigit: num_digits = num_digits + 1 if ch.isspace: num_space = num_space + 1arrow_forward
- Principles of Information Systems (MindTap Course...Computer ScienceISBN:9781285867168Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning




