0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonのprint関数

Last updated at Posted at 2018-11-13

#pythonのprint関数

##「退屈なことはpythonにやらせよう」
初心者である私が上記本を学習中につまずいたポイントを備忘録として残そうと思います。

###第4章 リスト

####文字絵グリッド(演習プロジェクト)

print関数の挙動の違い

成功したパターン

grid1.py
grid = [['.','.','.','.','.','.'],
         ['.','0','0','.','.','.'],
         ['0','0','0','0','.','.'],
         ['0','0','0','0','0','.'], 
         ['.','0','0','0','0','0'],
         ['0','0','0','0','0','.'],
         ['0','0','0','0','.','.'],
         ['.','0','0','.','.','.'],
         ['.','.','.','.','.','.']]

for y in range(len(grid[0])):
    for x in range(len(grid)):
        print(grid[x][y], end='')
    print()

結果

..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....

今回学んだことは、pythonにおいてインデントが非常に重要であること、および、しっかりとした理解をしなければエラーが簡単に生じてしまうことである。

失敗したパターン1

for y in range(len(grid[0])):
    for x in range(len(grid)):
        print(grid[x][y], end='')
..00.00...0000000..0000000...00000.....000.......0....

失敗したパターン2

for y in range(len(grid[0])):
    for x in range(len(grid)):
        print(grid[x][y], end='')
        print()
.
.
0
0
.
0
0
.
.
.
0
0
0
0
0
0
0
.
.
0
0
0
0
0
0
0
.
.
.
0
0
0
0
0
.
.
.
.
.
0
0
0
.
.
.
.
.
.
.
0
.
.
.
.
0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?