0

Currently I'm working in Python to try and simulate a set in a match of tennis based on player probabilities. There's a while loop that's never ending in my "ftsix" (firsttosixpoints) function, but I can't figure out what the bug is because my logic isn't sound. I've been working on the problem for a few hours but I can't seem to find the solution.

P.S - please play nice

#One set in tennis = 1st to 6 games
#One game = 1st to four points
#Serve switches after each game. 


def main():
    printIntro()
    probA, probB = getInputs()
    gamesA, gamesB = ftsix(probA, probB)
    if gamesA > gamesB:
        print"Player A has won with", gamesA, "to player B's", gamesB, "."
    else:
        print "Player B has won with", gamesB,"to player A's", gamesA, "."

def printIntro():
    print "This is a simulation of 1 tennis set based on player probabilities!" 

def getInputs():
    probA = input("What is the probability that player A will win?: ")
    probB = input("What is the probability that player B will win?: ")
    return probA, probB 

def ftsix(probA, probB):
    #First one to six games = one set
    x = 0
    gamesA = 0
    gamesB = 0
    while gamesA or gamesB != 6:
        if x % 2 == 0 or x == 0:
            serving = "A"
        else:
            serving = "B"
        #This is one game, not in a seperate function because I couldn't figure out how to change serve inside the function!
        pointsA = 0
        pointsB = 0
        while pointsA or pointsB != 4:
            if serving == "A":
                if probA >= random():
                    pointsA = pointsA + 1
                else:
                    pointsB = pointsB + 1
            if serving == "B":
                if probB >= random():
                    pointsB = pointsB + 1
                else:
                    pointsA = pointsA + 1
        if pointsA > pointsB:
            gamesA = gamesA + 1
        else:
            gamesB = gamesB + 1
        x = x + 1
    return gamesA, gamesB
Richard
  • 9
  • 3

2 Answers2

1

I think your syntax in the while loops is incorrect. You need a full conditional on each side of the or, for example:

while gamesA != 6 and gamesB != 6:
nbryans
  • 1,507
  • 17
  • 24
0

You need to change your loop to terminate when either gamesA or gamesB reaches 6:

while gamesA != 6 and games != 6:
niemmi
  • 17,113
  • 7
  • 35
  • 42
  • Thank you niemmi for pointing this out, I'm going to make that change right now and see how it works! – Richard Jun 15 '16 at 02:39