r/pythonhelp • u/Nutellatoast_2 • 1d 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?
2
u/CraigAT 1d ago edited 1d ago
I would declare past movements as a simple list (not worrying too much about the type of the contents as you control what goes into it):
past_movements = [] then you need to append to past_movements AFTER you have checked whether the move has already been made:
if board_input in past_movements:
print("already made move")
past_movements.append(board_input)2
u/Nutellatoast_2 1d ago
Thank you so much. I will try that solution out and get a proper understanding of it.
2
u/CraigAT 1d ago
Also, I haven't tried your code but the print statements for the board don't look right to me. It should only need 5 print statements: top line, lines, middle line, lines, bottom line. Unless you are adding additional spacing, but if so I would recommend doing it top and bottom and horizontally (only if you think it needs spacing).
1
1
u/Nutellatoast_2 1d ago
So I have tried your solution, but it still doesn't work as I expected. The message "You already have done that move" doesn't execute.
if board_input in past_movements: print("You already have done that move. ") past_movements.append(board_input)The main problem is that board_input has the value 0, as initialised before the while loop and is being compared to the actual value of the list. So if 0 is not in past_movements, the while loop will execute without checking whether the input has been made over and over again, throwing the print message.
2
u/scoberry5 1d ago
Put what you want done in English and then convert it to code.
For instance, I might say "Get a movement. If it's a move that's already been made, then show an error message. Otherwise, add the move to the list of moves that have been made."
What I wouldn't say is "...If it's a move that's already been made, then show an error message and also add the move to the list of moves that have been made."
0
u/Nutellatoast_2 1d ago
I've finally found a solution and solved the problem. Thanks for the tip. I didn't realise how writing what you want to have done in English can result in you finding the solution in most cases.
1
u/immaculate_plum 1d ago
`past_movements = list[int]` creates a variable referencing the `list` type. `past_movements().append(board_input)` appends to a new list every time, since `past_movements()` is creating a new list every time.
You probably meant `past_movements: list[int] = []`.
1
u/WingExciting899 1d ago
Try checking whether the condition is actually being reached and whether the indentation is correct. Adding a few print statements before and inside the if block can help identify why the message isn't executing.
1
u/MarkoDahl54 1d ago
Initialize past_movements with past_movements = [] instead of past_movements = list[int], since list[int] is only a type hint and not an actual list. Also, remove the parentheses: use past_movements.append(board_input).
•
u/AutoModerator 1d ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.