Type error when trying to print row number

时间:2015-12-14 17:53:13

标签: python python-2.7 debugging

I have this game of battleship that prints out a grid (row of 5 'o's printed 5x) and generates a random point for the user to guess.

These are the lines: 16-20

""" Allows the function to print row by row """
    def print_board(board):
        for row in board:
            for x in enumerate(row):
                print('%s " ".join(row)' % (x))

I'm getting these errors. But it was only after I changed line 20 to print the number alongside the printed grid (http://imgur.com/uRyMeLU) picture there

Traceback (most recent call last):   File "C:\Users\Jarrall\pythonPrac\battleship.py", line 23, in <module>
    print_board(board)   File "C:\Users\Jarrall\pythonPrac\battleship.py", line 20, in print_board
    print('%s " ".join(row)' % (x)) TypeError: not all arguments converted during string formatting

How would I get that piece of code to print a number (enumerating the length of the row list?) alongside the grid?

3 个答案:

答案 0 :(得分:1)

you are using enumerate wrong. I am not completely sure but it looks to me like you want it to print something like:

0 0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0

This can be done by enumerate(board) since enumerate returns an index and the iterator:

def print_board(board):
    for index,row in enumerate(board):
        print('{} {}'.format(index, ' '.join(row))

So that you can get:

>>> board = [['0' for _ in range(5)] for __ in range(5)]
>>> print_board(board)
0 0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0

EDIT - to add why your current print statement needed some fixin':

Your print statement doesnt do what you expected it to do. Let's walk through it:

print('%s " ".join(row)' % (x))
     #'                ' will create a string literal. This is obvious.
     # %s                a string formatting token meaning to replace with a string
     #                   % (x) the replacement for any of the string formatting
     #    " ".join(row)  this is your vital flaw. Although this is valid code, the print
     #                   statement will print this as literally `" ".join(row)
     #                   rather than actually running the code.

That is why you needed to change it to:

print('{} {}'.format(index, ' '.join(row))
#shout-out to jpmc26 for changing this

This replaces all instances of {} with the arguments given in format. You can learn more about the mini-language that has to do with string formatting here

答案 1 :(得分:0)

Taking a guess from your stack trace, I'd say you need to cast the x variable to a string, like this:

print('%s " ".join(row)' % (str(x)))

答案 2 :(得分:0)

Python's enumerate method returns a tuple of 0-indexed integers along with the list value. So rather than simply being a value in row, your x variable is actually (integer, row). If you just want to print a list of integers, change your inner loop to the following:

for x,y in enumerate(row):
    print('%s " ".join(row)' % (x))

This should solve your problem. If you want a more specific answer, please specify exactly what you are trying to do along with what the row variable is.