Python Never - 结束循环

时间:2016-06-15 02:24:32

标签: python while-loop

目前我正在使用Python来尝试根据玩家概率在网球比赛中模拟一组。有一段时间的循环永远不会在我的" ftsix" (firsttosixpoints)功能,但我无法弄清楚bug是什么,因为我的逻辑听起来并不合适。我已经在这个问题上工作了几个小时,但我似乎无法找到解决方案。

P.S - 请玩得好

#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

2 个答案:

答案 0 :(得分:1)

我认为你在while循环中的语法不正确。您需要在or的每一侧都有完整的条件,例如:

while gamesA != 6 and gamesB != 6:

答案 1 :(得分:0)

gamesAgamesB到达6时,您需要将循环更改为终止:

while gamesA != 6 and games != 6: