Python 如何在保持while循环的同时打破while循环?

Python 如何在保持while循环的同时打破while循环?,python,Python,我已经为一个Tic Tac Toe游戏制作了一个程序,我想这样做,当你输入一次磁贴时,它的占位符被填充,但是当你再次输入磁贴时,它将停止while循环。我该怎么做?这是我的密码: userInput = input("Chose a tile: 1, 2, 3, 4, 5, 6, 7, 8, 9.") tile1 = 0 tile2 = 0 tile3 = 0 tile4 = 0 tile5 = 0 tile6 = 0 tile7 = 0 tile8 = 0 tile9 = 0 Stanford

我已经为一个Tic Tac Toe游戏制作了一个程序,我想这样做,当你输入一次磁贴时,它的占位符被填充,但是当你再次输入磁贴时,它将停止while循环。我该怎么做?这是我的密码:

userInput = input("Chose a tile: 1, 2, 3, 4, 5, 6, 7, 8, 9.")
tile1 = 0
tile2 = 0
tile3 = 0
tile4 = 0
tile5 = 0
tile6 = 0
tile7 = 0
tile8 = 0
tile9 = 0
Stanford = 666
gameBoardMatrix = [
    ['1','2','3'], 
    ['4','5','6'],
    ['7','8','9']
]
while Stanford == 666:
  if userInput == '1':
    print ("You chose" + " " + gameBoardMatrix [0][0])
    tile1 = tile1 + 0.5
  if userInput == '2':
    print ("You chose" + " " + gameBoardMatrix [0][1])
  if userInput == '3':
    print ("You chose" + " " + gameBoardMatrix [0][2])
  if userInput == '4':
    print ("You chose" + " " + gameBoardMatrix [1][0])
  if userInput == '5':
    print ("You chose" + " " + gameBoardMatrix [1][1])
  if userInput == '6':
    print ("You chose" + " " + gameBoardMatrix [1][2])
  if userInput == '7':
    print ("You chose" + " " + gameBoardMatrix [2][0])
  if userInput == '8':
    print ("You chose" + " " + gameBoardMatrix [2][1])
  if userInput == '9':
    print ("You chose" + " " + gameBoardMatrix [2][2])  
  if tile1 == 1:
    print("Oh my, you seem to have broken the laws of physics. THE GAME IS ENDING! THE WORLD IS BROKEN!")
    break

您希望将棋盘的占用状态存储在类似于
gameBoardMatrix
的变量中,例如
is\u filled\u matrix

is_filled_matrix = [
    [False, False, False],
    [False, False, False],
    [False, False, False]
]
当用户选择单元格时,您可以更新
is\u filled\u matrix
以反映更改:

if user_input == '1':
    is_filled_matrix[0][0] = True
稍后,如果用户到达这个已经填充的单元格,您可以通过查看
is\u filled\u matrix
检查其占用情况。这可以通过如下修改代码来实现:

if user_input == '1':
    if is_full_matrix[0][0]:
        # The first cell has already been visited, we break
        break

    # The first cell has not been visited, we continue to play
# ...

根据我对您问题的理解,一旦有人两次选择9个磁贴中的任何一个,您就想退出while循环。
为此,您需要散列位置(平铺)和操作(单击)。
您可以使用列表或词典来完成这项工作。

使用列表:

使用字典:


我不确定我是否理解您的要求,但在我看来,您需要在while循环中请求用户输入,然后使用一系列if/else语句进行分支。
lst = [False] * 9
while True:
    tile = int(raw_input())
    if lst[tile]:
        break
    lst[tile] = True
d = {}
while True:
    tile = int(raw_input())
    if d.get(tile, False):
        break
    d[tile] = True