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.""")
CodePudding user response:
You can't list that index since it doesn't exist. If you want to reference a specific 'o' for each index, you need to turn each 'o' per string into its own list of o's.
board = []
for row in range(3):
board.append(["o"] * 3) <-- Put brackets
print(board)
def print_board(board):
for row in board:
print(" ".join(row))
print_board(board)
CodePudding user response:
The problem is your code is trying to replace an individual character in a string, but strings in python can not be edited.
If you run this code, form the beginning of your example, along with the aded print()
statement:
board = []
for row in range(3):
board.append("o" * 3)
print(board)
the output is ['ooo', 'ooo', 'ooo']
, which is a list of strings. This is how your board is actually represented in your program, although your print_board()
function shows it differently.
So when the line board[int(guess_row)][int(guess_column)] = "x"
is run, board[int(guess_row)]
first grabs just one string of ooo
, then [int(guess_column)]
grabs an individual o
and that is what causes the error, because you can't assign to a string.
You need to change the representation of your board so that it is a list of lists (or tuples would also work).
board = []
for row in range(3):
board.append(["o"] * 3)
print(board)
This prints: [['o', 'o', 'o'], ['o', 'o', 'o'], ['o', 'o', 'o']]
.