Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
3rd Edition
ISBN: 9781590282779
Author: John Zelle
Publisher: Franklin Beedle & Associates
bartleby

Videos

Question
Book Icon
Chapter 9, Problem 5PE
Program Plan Intro

Simulates the volleyball game

Program Plan:

  • Import the header file.
  • Define the “main” method.
    • Call the “printIntro ()” method
    • Call the “getInputs ()” method.
    • Call the “normalVolley ()” method.
    • Call the “rallyVolley ()” 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.
    • Get the player B possible for win from the user.
    • Get how many games to simulate from the user.
  • Define the “normalVolley()” method.
    • Set the values
    • Iterate “i” until it reaches “n” value
      • Call the method
      • Check “scoreA” is greater than “scoreB”
        • Increment the “winsA” value
      • Otherwise, increment the “winsB” value.
      • Return the value.
  • Define the “simOneMatch ()” method
    • Set the values
    • Check “winsA” and “winsB” is not equal to 2
      • Call the “simOneGame ()” method.
      • Check “scoreA” is greater than “scoreB”
        • Increment the “winsA” value
        • Increment the “x” value
      • Otherwise, increment the “winsB” value
        • Increment the “x” value
      • Return the result
  • Define the “simOneGame ()” method
    • Call the “findService ()” method
    • Set the values
    • Check the condition
      • If “serving” is equal to “A”
        • Check “random ()” is less than “probA”
          • Increment the “scoreA” value
        • Otherwise, set the value
      • Check “serving” is equal to “B”
        • Check “random ()” is less than “probB”
        • Increment the “scoreB” value
      • Otherwise, set the value.
      • Return the results.
  • Define the “findService ()” method
    • Check result of “x%2” is equal to 0
      • Return “A”
            • Otherwise, return “B”.
  • Define the “gameOver ()” method
    • Check “a” or “b” is greater than 15
      • Check “a-b” is greater than or equal to 2
        • Return true.
      • Otherwise, return false.
            • Otherwise, return false.
  • Define the “rallyVolley()” method.
    • Set the values
    • Iterate “i” until it reaches “n” value
      • Call the method
      • Check “scoreA” is greater than “scoreB”
        • Increment the “winsA” value
      • Otherwise, increment the “winsB” value.
      • Return the value.
  • Define the “simOneGame ()” method
    • Call the “findService ()” method
    • Set the values
    • Check the condition
      • If “serving” is equal to “A”
        • Check “random ()” is less than “probA”
          • Increment the “scoreA” value
        • Otherwise, set the value.
          • Increment the “scoreB” value.
      • Check “serving” is equal to “B”
        • Check “random ()” is less than “probB”
        • Increment the “scoreB” value
      • Otherwise, set the value.
        • Increment the “scoreA” value.
      • Return the results.
  • Define the “findService ()” method
    • Check result of “x%2” is equal to 0
      • Return “A”
            • Otherwise, return “B”.
  • Define the “gameOver ()” method
    • Check “a” or “b” is greater than 25
      • Check “a-b” is greater than or equal to 2
        • Return true.
      • Otherwise, return false.
            • Otherwise, return false.
  • Define the “printSummary ()” method
    • Display the results.
    • Calculate the “devA” and “devB” values
    • Check the condition.
      • Check “devA” is greater than 0
        • Display the result
      • Check “devA” is less than 0
        • Display the result.
      • Otherwise, display scoring difference
      • Check “devB” is greater than 0
        • Display the result.
      • Check “devB” is less than 0
        • Display the result
  • Call the main method.

Expert Solution & Answer
Check Mark
Program Description Answer

The program is to simulate a game of volleyball and investigate whether rally scoring reduces, magnifies, or has no effect on the relative advantage enjoyed by the better team.

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()

    winsA1, winsB1 = normalVolley(n, probA, probB)

    winsA2, winsB2 = rallyVolley(n, probA, probB)

    #call the method

    printSummary(winsA1, winsB1, winsA2, winsB2, n)

#definition of "printIntro" method

def printIntro():

    #display the statements

    print("This program is designed to compare the difference between Normal")

    print("and Rally scoring in a simulation of (n) games of Volleyball")

    print("given a probability (a number between 0 and 1) that reflects the")

    print("likelihood of a team winning the serve.")

#definition of "getInputs" method

def getInputs():

    #get the player A wins a serve

    a = eval(input("What is the prob. player A wins a serve? "))

    #get the player B wins a serve

    b = eval(input("What is the prob. player B wins a serve? "))

    #get the input from the user

    n = eval(input("How many games to simulate? "))

    #return the values

    return a, b, n

#definition of "normalVolley" method

def normalVolley(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

        scoreA, scoreB = simOneGame(probA, probB, n)

        #check "scoreA" is greater than "scoreB"

        if scoreA > scoreB:

            #increment the value

            winsA = winsA + 1

        #otherwise

        else:

            #increment the value

            winsB = winsB + 1

    #return the results

    return winsA, winsB

#definition of "simOneGame" method

def simOneGame(probA, probB, n):

   #call the method

    serving = findService(n)

    #set the values

    scoreA = 0

    scoreB = 0

    #check the condition

    while not gameOver(scoreA, scoreB):

        #check "serving" is equal to "A"

        if serving == "A":

            #check "random()" is less than "probA"

            if random() < probA:

                #increment the value

                scoreA = scoreA + 1

            #otherwise

            else:

                #set the value

                serving = "B"

        #check "serving" is equal to "B"

        if serving == "B":

            #check "random()" is less than "probB"

            if random() < probB:

                #increment the value

                scoreB = scoreB + 1

            #otherwise

            else:

                #set the value

                serving = "A"

    #return the result

    return scoreA, scoreB

#definition of "findService" method

def findService(x):

    #check the condition

    if x % 2 == 0:

        #return the value

        return "A"

    #otherwise

    else:

        #return the value

        return "B"

#definition of "gameOver" method

def gameOver(a, b):

    #check "a" or "b" is greater than 15

    if a > 15 or b > 15:

        #check "a-b" is greater than or equal to 2

        if abs(a-b) >= 2:

            #return the value

            return True

        #otherwise

        else:

            #return the value

            return False

    #otherwise

    else:

        #return the value

        return False

#definition of "rallyVolley" method

def rallyVolley(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

        scoreA, scoreB = simOneGame(probA, probB, n)

        #check "scoreA" is greater than "scoreB"

        if scoreA > scoreB:

            #increment the value

            winsA = winsA + 1

        #otherwise

        else:

            #increment the value

            winsB = winsB + 1

    #return the results

    return winsA, winsB

#definition of "simOneGame" method

def simOneGame(probA, probB, n):

   #call the method

    serving = findService(n)

    #set the values

    scoreA = 0

    scoreB = 0

    #check the condition

    while not gameOver(scoreA, scoreB):

        #check "serving" is equal to "A"

        if serving == "A":

            #check "random()" is less than "probA"

            if random() < probA:

                #increment the value

                scoreA = scoreA + 1

            #otherwise

            else:

                #set the value

                serving = "B"

                #increment the value

                scoreB = scoreB + 1

        #check "serving" is equal to "B"

        if serving == "B":

            #check "random()" is less than "probB"

            if random() < probB:

                #increment the value

                scoreB = scoreB + 1

            #otherwise

            else:

                #set the value

                serving = "A"

                #increment the value

                scoreA = scoreA + 1

    #return the result

    return scoreA, scoreB

#definition of "findService" method

def findService(x):

    #check the condition

    if x % 2 == 0:

        #return the value

        return "A"

    #otherwise

    else:

        #return the value

        return "B"

#definition of "gameOver" method

def gameOver(a, b):

    #check "a" or "b" is greater than 15

    if a > 25 or b > 25:

        #check "a-b" is greater than or equal to 2

        if abs(a-b) >= 2:

            #return the value

            return True

        #otherwise

        else:

            #return the value

            return False

    #otherwise

    else:

        #return the value

        return False

#definition of "printSummary" method

def printSummary(winsA1, winsB1, winsA2, winsB2, n):

    #display the results

    print("\nGames simulated: ", n)

    print("Results for Normal Volleyball to 15, score on own serve.")

    print("Wins for A: {0} ({1:0.1%})".format(winsA1, winsA1/n))

    print("Wins for B: {0} ({1:0.1%})".format(winsB1, winsB1/n))

    print()

    print("Results for Rally Volleyball to 25, score on any serve.")

    print("Wins for A: {0} ({1:0.1%})".format(winsA2, winsA2/n))

    print("Wins for B: {0} ({1:0.1%})".format(winsB2, winsB2/n))

    print()

    #calculate the "devA" value

    devA = (winsA2/n - winsA1/n)

    #calculate the "devB" value

    devB = (winsB2/n - winsB1/n)

    #check the condition

    if (winsA1 > winsB1) and (winsA2 > winsB2):

        #check "devA" greater than 0

        if devA > 0:

            #display the result

            print("Team A's advantage was magnified {0:0.1%} in their favor".format(devA))

            print("during the rally scoring match.")

        #check "devA" less than 0

        elif devA < 0:

             #display the result

            print("Team A's advantage was reduced {0:0.1%} in their favor".format(abs(devA)))

            print("during the rally scoring match.")

        #otherwise

        else:

             #display the result

            print("The scoring differences were not statistically significant.")

    #check the condition

    elif (winsB1 > winsA1) and (winsB2 > winsA2):

        #check "devB" greater than 0

        if devB > 0:

             #display the result

            print("Team B's advantage was magnified {0:0.1%} in their favor".format(devB))

            print("during the rally scoring match.")

        #check "devB" less than 0

        elif devB < 0:

             #display the result

            print("Team B's advantage was reduced {0:0.1%} in their favor".format(abs(devB)))

            print("during the rally scoring match.")

        #otherwise

        else:

             #display the result

            print("The scoring differences were not statistically significant.")

    #otherwise

    else:

         #display the results

        print("Neither team's advantage was statistically significant.")

        print("If the initial probabilities were near equal, it is impossible to")

        print("derive significance from the data.")

        print("Try a larger sample size.")

if __name__ == '__main__': main()

Sample Output

Output:

This program is designed to compare the difference between Normal

and Rally scoring in a simulation of (n) games of Volleyball

given a probability (a number between 0 and 1) that reflects the

likelihood of a team winning the serve.

What is the prob. player A wins a serve? 0.65

What is the prob. player B wins a serve? 0.6

How many games to simulate? 5000

Games simulated:  5000

Results for Normal Volleyball to 15, score on own serve.

Wins for A: 3312 (66.2%)

Wins for B: 1688 (33.8%)

Results for Rally Volleyball to 25, score on any serve.

Wins for A: 3325 (66.5%)

Wins for B: 1675 (33.5%)

Team A's advantage was magnified 0.3% in their favor

during the rally scoring match.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
: The Multithreaded Cook In this lab, we'll practice multithreading. Using Semaphores for synchronization, implement a multithreaded cook that performs the following recipe, with each task being contained in a single Thread: 1. Task 1: Cut onions. a. Waits for none. b. Signals Task 4 2. Task 2: Mince meat. a. Waits for none b. Signals Task 4 3. Task 3: Slice aubergines. a. Waits for none b. Signals Task 6 4. Task 4: Make sauce. a. Waits for Task 1, and 2 b. Signals Task 6 5. Task 5: Finished Bechamel. a. Waits for none b. Signals Task 7 6. Task 6: Layout the layers. a. Waits for Task 3, and 4 b. Signals Task 7 7. Task 7: Put Bechamel and Cheese. a. Waits for Task 5, and 6 b. Signals Task 9 8. Task 8: Turn on oven. a. Waits for none b. Signals Task 9 9. Task 9: Cook. a. Waits for Task 7, and 8 b. Signals none At the start of each task (once all Semaphores have been acquired), print out a string of the task you are starting, sleep for 2-11 seconds, then print out a string saying that you…
Programming Problems 9.28 Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a C program that is passed a virtual address (in decimal) on the command line and have it output the page number and offset for the given address. As an example, your program would run as follows: ./addresses 19986 Your program would output: The address 19986 contains: page number = 4 offset = 3602 Writing this program will require using the appropriate data type to store 32 bits. We encourage you to use unsigned data types as well. Programming Projects Contiguous Memory Allocation In Section 9.2, we presented different algorithms for contiguous memory allo- cation. This project will involve managing a contiguous region of memory of size MAX where addresses may range from 0 ... MAX - 1. Your program must respond to four different requests: 1. Request for a contiguous block of memory 2. Release of a contiguous block of memory 3. Compact unused holes of memory into one single block 4.…
using r language
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole
Text book image
A+ Guide To It Technical Support
Computer Science
ISBN:9780357108291
Author:ANDREWS, Jean.
Publisher:Cengage,
Text book image
Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781285867168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
The Top Down Approach to Software Development; Author: Christopher Kalodikis;https://www.youtube.com/watch?v=v9M8LA2uM48;License: Standard YouTube License, CC-BY