Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Getting error when guessing wrong on battleship style game

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.""")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading