Python 检查是否已经采取了Tic Tac Toe位置

Python 检查是否已经采取了Tic Tac Toe位置,python,python-3.x,tic-tac-toe,Python,Python 3.x,Tic Tac Toe,这是在我的程序中工作的代码的一个简短示例 Board = [1,2,3,4,5,6,7,8,9] print(Board[0], '|', Board[1], '|', Board[2]) print(Board[3], '|', Board[4], '|', Board[5]) print(Board[6], '|', Board[7], '|', Board[8]) Player_Input = int(input("Choose a number between 1-9: &qu

这是在我的程序中工作的代码的一个简短示例

Board = [1,2,3,4,5,6,7,8,9]
print(Board[0], '|', Board[1], '|', Board[2])
print(Board[3], '|', Board[4], '|', Board[5])
print(Board[6], '|', Board[7], '|', Board[8])
Player_Input = int(input("Choose a number between 1-9: "))
while Board[Player_Input - 1] == Player_Input:
    Board[Player_Input - 1] = 'X'
我试着用这两行来检查一个位置是否已经被占据,但不知怎么的,它不起作用。我也尝试过这样做:

while Board[Player_Input - 1] != Player_Input:
    Player_Input = int(input("Already taken. Choose another position: "))

我认为主要的问题是您试图使用
while
循环将
'X'
分配到他们选择的位置。
while
循环的目的不是对列表中的某个特定已知位置执行某些操作;只要满足一定的条件,就要重复做某件事。在您的例子中,您知道播放器选择了哪个空间(基于
player\u Input
的值),因此使用
while
循环不是正确的方法。 相反,只需检查所选的空格(
Board[Player\u Input-1]
)是否尚未被占用,然后分配值

比如:

while Board[Player_Input - 1] == 'X' or Board[Player_Input - 1] == 'O':
    Player_Input = int(input("Already taken. Choose another position: "))
编辑:

我想我现在可以明白你在循环中的意图了——你想要“重复,直到他们做出正确的选择”。一种选择是将我在上面编写的代码用以下内容包围:

if Board[Player_Input - 1] != 'X' and Board[Player_Input - 1] != 'O':
   Board[Player_Input - 1] = 'X'  # assuming 'X' is the human player
else:
    # Handle the case where the space is full here.
编辑2:

这也应该起作用,也许更接近您的预期:

while True: # Loop forever until we break out of it
    # Above code here
    if Board[Player_Input - 1] == 'X':
        break
Board=[1,2,3,4,5,6,7,8,9]
计数=0
玩家输入=int(输入(“在1-9之间选择一个数字:”)
如果播放器输入<10且播放器输入>=1:
对于范围内的玩家(9):
如果棋盘[玩家输入-1]=“X”或棋盘[玩家输入-1]=“O”:
打印('您不能使用此号码')
如果计数%2==0:
棋盘[玩家输入-1]=“X”
其他:
棋盘[玩家输入-1]=“O”
def show():
打印(板[0]、“|”、板[1]、“|”、板[2])
打印(电路板[3]、“|”、电路板[4]、“|”、电路板[5])
打印(电路板[6]、“|”、电路板[7]、“|”、电路板[8])
打印(显示())
Player_Input=int(输入(“已采取。选择其他位置:”)
计数=计数+1

它如何“不起作用”?是有错误,还是什么都没做,还是别的什么?不,没有错误。如果我输入1并且位置1已经被占据,它甚至不会要求我输入新的号码。它停留在那里,就像是一个无止境的循环。显示的代码片段2和3不会悄悄地进入无止境的循环。问题(至少部分)不在这些范围之内。这里的问题是你根本不应该使用循环——更多细节见下面我的答案。我想我明白你的意思了。。你想“重复,直到他们做出正确的选择。”但这不是你写的。我将对我的答案进行更多的编辑,以澄清这一点。非常感谢:D这帮助我解决了“将电子邮件发送到StackOverflow”的问题。虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请编辑您的答案,添加解释,并说明适用的限制和假设。
# Player chooses a space
Player_Input = int(input("Choose a number between 1-9: "))

# Available spaces should contain the space number, not 'X' or 'O'
# while choice not available: have player choose again
# (Should also add a check for if the input is not 1-9)
while Board[Player_Input - 1] != Player_Input:
    Player_Input = int(input("Already taken. Choose another position: "))

# Now that we know they made a valid choice, we fill in the players letter
Board[Player_Input - 1] = 'X'
Board = [1,2,3,4,5,6,7,8,9]
count = 0
Player_Input = int(input("Choose a number between 1-9: "))
if Player_Input < 10 and Player_Input >= 1:
    for player in range(9):
        if Board[Player_Input - 1] == 'X' or Board[Player_Input - 1] == 'O':
            print('you cant use this number')
        if count % 2 == 0:
            Board[Player_Input - 1] = 'X'
        else:
            Board[Player_Input - 1] = 'O'
        def show():
            print(Board[0], '|', Board[1], '|', Board[2])
            print(Board[3], '|', Board[4], '|', Board[5])
            print(Board[6], '|', Board[7], '|', Board[8])
        print(show())
        Player_Input = int(input("Already taken. Choose another position: "))
        count = count + 1