通过“ While”循环运行列表后,在“ While”循环外修改列表

时间:2018-11-27 16:32:16

标签: python list

我目前正在尝试从头开始创建自己的TicTacToe游戏,并且目前在遇到与创建的列表相对应的“ while”循环时遇到了困难。 我正在使用以下列表:

board = [1,2,3,4,5,6,7,8,9]

标记3x3 TicTacToe游戏中的所有9个插槽。 但是,当玩家一举棋(例如,在插槽'1'中)时,该列表应更改为显示

board = [X,2,3,4,5,6,7,8,9]

这应该一直持续到列表中的所有9个索引(我相信是适当的术语)都应被'X'或'O'占据,这将等于游戏中的并列!

就目前而言,我只是在做实验,请原谅其余代码,但我使用的完整代码是:

board = [1,2,3,4,5,6,7,8,9]
def CreateBoard(board):
    print('   |   |')
    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
    print('   |   |')

PlayerOne = 'X'
Turn = 'player one'
GameRunning = True



while [0] == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 in board == True:
    if Turn == 'player one':
       letter = 'X'
       Move = input("Please pick where to go next: ")
       Move = int(Move)
       if Move in board:
          board.insert(Move, letter)
          board.remove(Move)
          print(board)
          Turn = 'player two'




       else:
          print("This move is invalid")

  if Turn == 'player two':
     letter = 'O'
     Move = input("Pick where to go next: ")
     Move = int(Move)
     if Move in board:
        board.insert(Move, letter)
        board.remove(Move)
        print(board)
        Turn = 'player one'


     else:
         print("This move is invalid")

我得到的输出是: enter image description here

我猜想while循环正在运行循环外的列表,但是我试图找到一种方法来更改它! 我还没有弄清楚为什么打印它的“那一步是无效的”!

1 个答案:

答案 0 :(得分:2)

while循环的问题在于,始终将非零整数视为“ true”。因此

Error: Couldn't link model to 'en_core_web_md'
Creating a symlink in spacy/data failed. Make sure you have the required
permissions and try re-running the command as admin, or use a
virtualenv. You can still import the model as a module and call its
load() method, or create the symlink manually.

C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\en_core_web_md
-->
C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\spacy\data\en_core_web_md


Creating a shortcut link for 'en' didn't work (maybe you don't have
admin permissions?), but you can still load the model via its full
package name: nlp = spacy.load('{name}')
Download successful but linking failed


(cha_env36) C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation>
(cha_env36) C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation>python -m spacy link en_core_web_md en

Error: Couldn't link model to 'en'
Creating a symlink in spacy/data failed. Make sure you have the required
permissions and try re-running the command as admin, or use a
virtualenv. You can still import the model as a module and call its
load() method, or create the symlink manually.

C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\en_core_web_md
-->
C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\spacy\data\en

Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
  File "C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\spacy\__main__.py", line 31, in <module>
plac.call(commands[command], sys.argv[1:])
  File "C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\plac_core.py", line 328, in call
cmd, result = parser.consume(arglist)
  File "C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\plac_core.py", line 207, in consume
return cmd, self.func(*(args + varargs + extraopts), **kwargs)
  File "C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\spacy\cli\link.py", line 48, in link
symlink_to(link_path, model_path)
  File "C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation\cha_env36\lib\site-packages\spacy\compat.py", line 87, in symlink_to
orig.symlink_to(dest)
  File "C:\Python36\lib\pathlib.py", line 1327, in symlink_to
self._accessor.symlink(target, self, target_is_directory)
  File "C:\Python36\lib\pathlib.py", line 393, in wrapped
return strfunc(str(pathobjA), str(pathobjB), *args)
OSError: symbolic link privilege not held

(cha_env36) C:\Users\antoi\Documents\Programming\Nathalie\Chatbot_RASA_room_reservation>sudo python -m spacy link en_core_web_md en
C:\Python36\python.exe: No module named spacy

实际上是指“而包含0的列表等于整数1(始终为false),2(true)或3(true)...或9(inboard)中的整数(如果9仍在列表中,则为true)板),然后输入相应的代码块。”

我相信您的意思是更多:

while [0] == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 in board == True:

这意味着当板上的任何单元格都在1(含)到10(不含)范围内时,然后输入块。

看看这段代码:

while any((cell in range(1,10) for cell in board)):