Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python的打印与返回缩进规则_Python - Fatal编程技术网

python的打印与返回缩进规则

python的打印与返回缩进规则,python,Python,考虑一下这个代码 board = [] for x in range(0, 5): board.append(["O"] * 5) def print_board(board): for row in board: x=" ".join(row) print x return x board = [] for x in range(0, 5): board.append(["O"] * 5) def print_board

考虑一下这个代码

board = []
for x in range(0, 5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        x=" ".join(row)
        print x
        return x
board = []
for x in range(0, 5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        x=" ".join(row)
        print x
    return x
这就产生了OOOO

这个密码呢

board = []
for x in range(0, 5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        x=" ".join(row)
        print x
        return x
board = []
for x in range(0, 5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        x=" ".join(row)
        print x
    return x
产生

OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

有人能解释一下原因吗?

在第一个函数中,在第一次迭代后,您将
返回
ing出循环,因此不再运行
print
语句


这是Python“缩进敏感度”的一个演示,在其他语言中,由于大括号的存在,这可能不会有什么不同;在Python中是这样的。

函数一碰到
return
语句就终止。在第一个示例中,这发生在第一个循环中。在第二个循环中,它发生在最后一个循环之后。