Forum Archive

Codes does not work in Pythonista

theotherswan

Please help. The following codes work fine on other Python platforms but not in Pythonista on my iPhone 8:

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

for column in len(grid[0]):
    print()
    for row in len(grid):
        print(grid[column][row])

print()

Please help.

dgelessus

What exactly isn't working when you run the code? Do you get an error? If so, please tell us what the error message is, otherwise it's very hard to find out what's going wrong. If possible, copy and paste the exact error message that you get.

mikael

@theotherswan, your code should work. Are you running it with Python 2 and getting an error on the py3-style print statements? Or lost the indentation when copying the code over?

ccc

More Pythonic to write...

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

for column in grid:
    print()
    for row in column:
        print(row)

print()
mikael

@ccc, swap ”column” and ”row” for a better mental picture?

ccc

Even better...

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

for row in grid:
    print('  '.join(row))

print()