dawibo
dawibo

Reputation: 3

Code infinitely runs loop and I don't see why

I feel like the move_spot variable should reset to the value '_' after each iteration of the loop, but the loop infinitely runs without asking the user for an input after the first iteration. I don't see where the bug is, and it's even more confusing for me since the first iteration works as I want. (The variables and functions are all in previous code, but the problem is somewhere in this block, so I didn't want to post the full code)

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)

Upvotes: 0

Views: 70

Answers (1)

RufusVS
RufusVS

Reputation: 4127

Let's try copying your code, and note the changed line so you see the looping line:

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)

See now?

Upvotes: 1

Related Questions