I'm working through Python Programming by Zelle. I've entered the code for the racquetball problem in Chapter 9 exactly as written in the text. (At least I think so.) The program returns one game only then quits. I've prototyped and unit tested but can't find my mistake. Any insights would be helpful. Here is my code:

From random import random
def main():
    #print and introduction
    printIntro()
    #get the inputs:  probA, probB,n
    probA,probB,n=getInputs()
    #simulate n games of racquetball using probA and probB
    winsA, winsB=simNGames(n,probA,probB)
    #print a report on the wins for playerA and playerB
    printSummary(winsA,winsB)

def printIntro():
    print("This program simulates a game of racquetball between two")
    print("players called A and B.  The abilities of each player are")
    print("indicated by a probability(a number between 0 and 1) that")
    print("the player wins the point when serving.  Player A always")
    print("has the first serve.")

def getInputs():
    #returns the three simulation parameters, probA, probB and n
    a=eval(input("What is the probability playerA wins a serve?  "))
    b=eval(input("What is the probability playerB wins a  serve?  "))
    n=eval(input("How many games do you want to simulate?  "))
    return a,b,n

def simNGames(n,probA, probB):
    #Initialize winsA and winsB to 0
    winsA=winsB=0
    #loop n times
    for i in range(n):
        #simulate a game
        scoreA, scoreB=simOneGame(probA, probB)
        #if playerA wins
        if scoreA>scoreB:
            #Add one to wins A
            winsA=winsA+1
        #else
        else:
            winsB=winsB+1
        return winsA, winsB

def simOneGame(probA, probB):
    #initialize scores to 0
    serving="A"
    scoreA=0
    scoreB=0
    #set serving to A
    #Loop while the game is not over:
    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):
    #a and b represent scores for a racquetball game
    #Returns True if the game is over, False otherwise.
    return a==15 or b==15

def printSummary(winsA,winsB):
    #prints a summary of wins for each player
    n=winsA+winsB
    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))

Typically that means that a return statement is indented incorrectly. Check all of your return statements.

Will do. thank you.

Yes, that was the problem. It worked immediately. Hours and hours of head-banging were resolved by one proper indent. Grin. Thanks again.

We've all been there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.