Error with my extended battleships game (Extra Credit excercise) - Solved


#1

I've been challenging myself to improve my game and clean up some bits of code but I'm getting an error and can't see what I've done wrong. If anyone could explain why it's having problems, that'd be really helpful (if you want to point out unneccesary bits of code as well, I'd appreciate it!) Sorry for the excessive amount of 'print'ing, I like my code to look pretty and have nice spacing :smile:

My code (import random is part of it):
` import random

print "Welcome to Battleship!"
print
print "You have 10 turns to find the ship."
print
print "Before you start, here's some tips:"
print
print "Remember that 0 is the first line/column, not 1."
print
print "It's possible to enter coordinates outside the grid, this will not affect your turn count."
print
print "Even if you enter the same coordinate twice it will not affect your turn count."
print
print "You do have a limit of 25 turns in total so don't make too many mistakes!"
print
Player_Status = raw_input("Ready? Type 'Yes' to play or 'No' to repeat tips:")
print
print

while Player_Status != "Yes":
    while Player_Status == "No":
        print "You have 10 turns to find the ship."
        print
        print "Before you start, here's some tips:"
        print
        print "Remember that 0 is the first line/column, not 1."
        print
        print "It's possible to enter coordinates outside the grid, this will not affect your turn count."
        print
        print "Even if you enter the same coordinate twice it will not affect your turn count."
        print
        print "You do have a limit of 25 turns in total so don't make too many mistakes!"
        print
        Player_Status = raw_input("Ready? Type 'Yes' to play or 'No' to repeat tips:")
        print
        print
    while (Player_Status != "Yes") and (Player_Status != "No"):
        print "Invalid response."
        print
        Player_Status = raw_input("Ready? Type 'Yes' to play or 'No' to repeat tips:")
        print
        print

board = ["O" * 5] * 5
def print_board(board):
    for row in board:
        print " ".join(row)

ship_row_1 = random.randint(0,4)
ship_col_1 = random.randint(0,4)

print "Let's play!"
print

turn = 1

for number in range(1, 25):
    print "Turn ", turn
    print_board(board)
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Column:"))

    if turn == 10:
        print "Out of turns."
        print "You lose."
        print "Game Over."
        break

    elif guess_row == ship_row_1 and guess_col == ship_col_1:
        print "Congratulations! You sunk my battleship!"
        print "You completed the game in ",turn," turns."
        print "Game Over."
        break

    elif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        print "Invalid coordinates, try again."
        print

    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that coordinate already, try again."
        print

    else:
        print "Miss."
        print
        turn = turn + 1
        board[guess_row][guess_col] = "X"`

When I guess something that is within the grid and isn't correct (so is following the 'else' path) I get this error:

Traceback (most recent call last):
File "python", line 94, in
TypeError: 'str' object does not support item assignment

Line 94 is the last line: board[guess_row][guess_col] = "X"
I don't understand what it is that I've edited that would have caused this to break as it works fine on the actual Codecadamy lessons. Any help is greatly appreciated, thanks in advance!

Cevana~


#2

Strings are immutable, you cannot change individual characters, only replace the whole string.
Use lists if you want to be able to change out elements.


#3

Thanks so much, I must've replaced the brackets with square brackets by accident!