Код бесконечно запускает l oop, и я не понимаю, почему - PullRequest
0 голосов
/ 05 августа 2020

Мне кажется, что переменная move_spot должна сбрасываться до значения '_' после каждой итерации l oop, но l oop работает бесконечно, не запрашивая у пользователя ввод после первой итерации. Я не вижу, в чем ошибка, и меня это еще больше сбивает с толку, поскольку первая итерация работает так, как я хочу. (Переменные и функции все в предыдущем коде, но проблема где-то в этом блоке, поэтому я не хотел публиковать полный код)

while gameOver == False:
    print('\nWhere would you like to move?\nYou can chose top-, mid-, or bot- with L, M, or R at the end')
    move_spot = '_' # I feel like this should reset move_spot every time the loop runs, meaning the user has to give an input
    while move_spot not in theBoard.keys():
        move_spot = input()
    theBoard[move_spot] = userChar
    comp_spot = '_'
    while move_spot not in theBoard.keys():
        comp_spot = random.choice(list(theBoard))
    theBoard[comp_spot] = compChar

    printBoard(theBoard)

1 Ответ

1 голос
/ 05 августа 2020

Давайте попробуем скопировать ваш код и обратите внимание на измененную строку, чтобы вы увидели зацикленную строку:

while gameOver == False:
       print('\nWhere would you like to move?\nYou can chose top-, mid-, or bot- with L, M, or R at the end')
       move_spot = '_' # I feel like this should reset move_spot every time the loop runs, meaning the user has to give an input
       while move_spot not in theBoard.keys():
           move_spot = input()
       theBoard[move_spot] = userChar
       comp_spot = '_'
       while comp_spot not in theBoard.keys():   # <-- changed this line to comp_spot
           comp_spot = random.choice(list(theBoard))
        theBoard[comp_spot] = compChar

        printBoard(theBoard)

Смотрите сейчас?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...