簡體   English   中英

我的計數器不會在Python中停止

[英]My counter won't stop in Python

我不知道為什么,但是我的計數器不會停止,盡管我認為我的編碼正確,可以在三個迭代后停止。 關於它為什么行不通的任何想法? @Ayush在程序開始時,計數器將初始化為1,此處未包括在內。 我將把條件放在哪里? 正如我嘗試同時使用“用於range(3)中的counter和用於range(3)中的while計數器,但是在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

@ Cyber​​Geekk.exe這是“轉彎”的描述,轉彎= 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次。

希望這對您的項目有所幫助,如果需要更多幫助,請發表評論。 別客氣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM