While 和 continue 问题

While and continue issue

你能帮我理解我做错了什么吗? 我在下面有两个列表,我需要 x 是否为 !=' ' in board => 继续输入 x 直到它为 ' '。提前谢谢你。

board = [' ', ' ', 'X', ' ', ' ', ' ']
testlist_1 = ['x','y', 'z']

x = int(input('>'))

while True:
    if board[x] == ' ':
        for i in range (len (testlist_1)):
            board[x] = testlist_1[i]
            x += 1
            if board[x] !=' ':
             break
    else:
       x = int (input ('>'))
       continue


   #I need:
   #input:x = 2
   #board = [' ', ' ', 'X', ' ', ' ', ' ']
   #ask x 
   #input:x =0
   #board = ['x', 'y', 'X', ' ', ' ', ' ']

试一试,我添加了一些打印语句,这样您就可以看到电路板在每个循环中的样子:

board = [' ', ' ', 'X', ' ', ' ', ' ']
testlist_1 = ['x','y', 'z']

while True:
    x = int(input('>'))
    if board[x] == ' ':
        for test_item in testlist_1:
            board[x] = test_item
            x += 1
            if board[x] !=' ':
                break
        break
    print(board)
print(board)

输入输出:

>2
[' ', ' ', 'X', ' ', ' ', ' ']
>0
['x', 'y', 'X', ' ', ' ', ' ']