I am trying to make a battleship type of game where there is a 3 by 3 board with one random point on the board that you are trying to guess. You should have 2 tries to guess the point, with an incorrect guess leaving an x in place of the o on the board.
The error is for this line:
board[int(guess_row)][int(guess_column)] = "x"
It says TypeError: 'str' object does not support item assignment
.
board = []
for row in range(3):
board.append("o" * 3)
def print_board(board):
for row in board:
print(" ".join(row))
print_board(board)
stone_row = randint(1, 3)
stone_column = randint(1, 3)
print(stone_row)
print(stone_column)
for turn in range(2):
response_row = 0
while response_row == 0:
guess_row = input("""
Cup Man: So, what row do you guess?
""")
valid_cup = ["1", "2", "3"]
if guess_row in valid_cup:
response_row = 1
response_column = 0
while response_column == 0:
guess_column = input("""
Cup Man: And what column?
""")
if guess_column in valid_cup:
response_column = 1
if int(guess_row) == stone_row and int(guess_column) == stone_column:
print("""
The man lifts up the cup that you guessed.""")
input("")
print("""
The stone is there!""")
input("")
print("""
Cup Man: Guess you win. Here's 60 gold.""")
gold += 60
break
else:
if (int(guess_row) < 1 or int(guess_row) > 3) or (int(guess_column) < 1 or int(guess_column) > 3):
print("""
Cup Man: That's not right...""")
elif board[int(guess_row)][int(guess_column)] == "x":
print("""
Cup Man: You just guessed that one genius...""")
else:
print("""
The man lifts up the cup that you guessed.""")
input("")
print("""
There's nothing there.""")
input("")
print("""
Cup Man: Too bad. Wrong choice.""")
board[int(guess_row)][int(guess_column)] = "x"
turn += 1
print_board(board)
if turn == 2:
print("""
Cup Man: That's it. You lose.""")