Whats wrong with my code? (Battleship, Make a list)



Traceback (most recent call last):
File "python", line 4, in
TypeError: can only concatenate list (not "str") to list

So this code

board = []

for i in range(0,5):
    board.append(["O"] * 5)

lets me pass the exercise, but it doesn't make a 5 x 5 grid, only a line of 25 O's

I tried this one, so that it adds a new line after each 5er row of O's, but it doesnt work

board = []

for i in range(0,5):
    board.append((["O"] * 5 + "\n"))

What am I doing wrong? \n should work inside a string?

edit: Ok I am now a bit more ahead where you call a function which iterates through every 5 O's and prints them on new lines. The 5x5 works now, but my question remains, why doesn't the + "\n" work?


because ["O"] * 5 is not a string, its a list.

You will work on printing the board later


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.