I'm trying to create a very simple tictactoe game.
I have this if-statement which dosen't seem to work as I want:
#Defining the board. 3 rows with 3 cells
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
#Player icons
p1_icon = "X"
p2_icon = "O"
#Player input
p1_input_row = 0
p1_input_cell = 0
p2_input_row = 0
p2_input_cell = 0
#Player input text
p1_turn = "Player 1's turn: "
p2_turn = "Player 2's turn: "
#String for invalid answer
invalid = "Invalid answer! Try again."
#Function for printing the board
def print_board():
print("-------")
print("|" + str(board[0][0]) + "|" + str(board[0][1]) + "|" + str(board[0][2]) + "|")
print("-------")
print("|" + str(board[1][0]) + "|" + str(board[1][1]) + "|" + str(board[1][2]) + "|")
print("-------")
print("|" + str(board[2][0]) + "|" + str(board[2][1]) + "|" + str(board[2][2]) + "|")
print("-------")
#Main game-loop
while True:
#Player 1's turn
print(p1_turn)
p1_input_row = int(input("Row: "))
#Check player 1's input
if p1_input_row == 1 or 2 or 3:
p1_input_cell = int(input("Cell: "))
if p1_input_cell == 1 or 2 or 3:
board[(p1_input_row - 1)][(p1_input_cell - 1)] = p1_icon
print_board()
else:
print(invalid)
elif p1_input_row == 4:
print("WTF")
else:
print(invalid)
break
I want to check if the player (p1_input_row
) choose a row between 1 and 3. If so, then the program should ask the player to choose a cell: p1_input_cell = int(input("Cell: "))
. If the player didn't choose a row between 1 and 3, then display a error message. The same if-statement goes for p1_input_cell
.
When I write 4
for example, then the error message dosn't display. The program asks for the cell instead. Why is that?
Aucun commentaire:
Enregistrer un commentaire