我的计数器不会在 Python 内停止

My counter won't stop in Python

我不知道为什么,但我的计数器不会停止,尽管我认为我已将其编码为在三次迭代后停止。关于为什么它不起作用的任何想法? @Ayush 计数器在程序开始时被初始化为 1,此处未包括在内。我将把条件放在哪里?正如我尝试同时使用“for counter in range(3) 和 while counter in range(3) 但是在 while 计数器的情况下它运行 3 次迭代然后开始第四次这给了我一个错误说:

Traceback (most recent call last):
  File "C:\Python32\Final Year Project\Build 6 (MiniMax input).py", line 311, in <module>
    print('The ' + turn + ' has been chosen this time, what move will they make first?!.')
TypeError: Can't convert 'NoneType' object to str implicitly

@CyberGeekk.exe 这是对 'turn' 的描述,作为 turn = whoGoesFirst() 我也将其包括在内。

def whoGoesFirst():
     if (counter==1):
          if random.randint(1,1) == 0:
              return 'computer'
          else:
              return 'player'
     if (counter==2):
          if random.randint(1, 1) == 0:
              return 'computer'
          else:
              return 'player'
     if (counter==3):
          if random.randint(1, 1) == 0:
              return 'computer'
          else:
               return 'player'

theBoard = [' '] * 10
playerLetter, computerLetter = inputPlayerLetter()
turn = whoGoesFirst()
print('Now For Level ' +str(counter)+ '!!')
print('--------------------')
print('The ' + str(turn) + ' has been chosen to go first, what will they do?') 
gameIsPlaying = True

while gameIsPlaying:
    while counter in range(3):
    if turn == 'player':
        # Player's turn.
        drawBoard(theBoard)
        move = getPlayerMove(theBoard)
        makeMove(theBoard, playerLetter, move)

        if isWinner(theBoard, playerLetter):
            counter=counter+1
            drawBoard(theBoard)
            print('--------------------')
            print("No, no! It cannot be! Somehow you tricked me, human. \n" 
                  "But never again! I, the computer,will increse my skill level for round "+str(counter)+" and beat you")
            print('--------------------')
            print('Now For Level ' +str(counter)+ '!!')
            #print('-------------------')
            gameIsPlaying = True
            theBoard = [' '] * 10
            playerLetter, computerLetter = inputPlayerLetter()
            turn = whoGoesFirst()
            print('The ' + turn + ' has been chosen this time, what move will they make first?!.')
            gameIsPlaying = True

循环在 if (counter<=4): 处中断,因为 1 小于 4。要使循环进行 3 次,只需将该行的代码更改为如下所示:if (counter == 4):。我在注释掉所有未提供的方法时尝试了这个,并使循环进行了 3 次左右。

希望这对您的项目有所帮助,如果您需要更多帮助,请发表评论。不客气。