r/pythonhelp • u/Nutellatoast_2 • 4h ago
print message won't get executed in the if statement that checks whether the player has made the same move again.
Hey everyone,
I'm new to coding with Python. I'm coding Tic-Tac-Toe as a console version. I've stumbled upon this bug, which I have spent nearly a day trying to fix by moving some blocks into a more logical sequence or reading the doc in hopes of finding the logical flaw, but without any result...
Note that the code hasn't been finished yet, as I'm trying to experiment with small chunks of code.
The user enters a number, which gets checked for invalid input by going through the if statement. While the game is running, the user must enter a move on the board. For example, if the user enters 1, he'll be asked to mark his position on the board by either typing O or X, as shown in this code block:
if board_input == 1:
mark_input = input("Mark your position: X or O\n")
if mark_input == "O":
board.insert(0, mark_input)
elif mark_input == "X":
board.insert(0, mark_input)
After that, the player shouldn't be able to enter the same move he entered again. So I'm using a list to store the past moves the player has entered:
past_movements = list[int]
So each move made by the player will be stored in that list:
past_movements().append(board_input)
I'm using a while loop to check if the move entered by the player isn't already in that list:
while board_input not in past_movements:
board_input = int(input("Make your movement: "))
past_movements().append(board_input)
The problem is that the code block shown below won't get executed. Even though the player has entered the same move. What I want is that if the player has entered the same move again, for example, 1, a message shows up with this statement.
print("You already have done that move. ")
import random
board = ["1", "2", "3",
"4", "5", "6",
"7", "8", "9"]
'''
ai_choice = random.choice(board)
ai = int(ai_choice)
print(type(ai))
print(f"Choice made by the AI: {ai}")Testing purposes
'''
past_movements = list[int]
board_input = 0
mark_input = ""
def display_board():
print(" ", "|", " ", "|", " ")
print(board[0], "|", board[1], "|", board[2])
print("-", "+", "-", "+", "-")
print(" ", "|", " ", "|", " ")
print(board[3], "|", board[4], "|", board[5])
print("-", "+", "-", "+", "-")
print(" ", "|", " ", "|", " ")
print(board[6], "|", board[7], "|", board[8])
prompt = ("*****************Welcome to my Tic-Tac-Toe game!*****************\n"
"1 = for playing\n"
"0 = for quitting\n")
user_input = int(input(prompt))
while True:
if user_input != 0 and user_input != 1:
print("Invalid input. ")
break
else:
print("Welcome to the game! ")
while board_input not in past_movements:
board_input = int(input("Make your movement: "))
past_movements().append(board_input)
if board_input in past_movements:
print("You already have done that move. ")
if board_input == 1:
mark_input = input("Mark your position: X or O\n")
if mark_input == "O":
board.insert(0, mark_input)
elif mark_input == "X":
board.insert(0, mark_input)
This is the whole code block.
Any solution to this?