我正在阅读有关python的教程此处,当时想知道如果for循环中没有像{}这样的代码块,我们如何知道for循环中的哪个代码块.我们是否必须根据代码的缩进来阅读它?还是我错过了一些有关python的基本知识?当我在for循环中尝试在notepad ++中尝试一些python代码并在代码中间创建新行时,由于某种原因,它使代码行及其上方的所有内容均成为代码块,而下面的其他所有内容有些不同.我又想念什么吗?我希望这不是不好的编程习惯.
I was reading the tutorial about python here and was wondering that if the for loop does not have a block like this {}, how would we know which block of code is in the for loop. Are we going to have to read it base on the indentation of the code? Or did I miss something fundamental about python? And while I was trying out some python code in notepad++ when I was in the for loop and create a new line in the middle of my code it for some reason made the line of code and everything above it a block of code while everything else below something different. Again am I missing something? I hope it's not bad programming practice.
推荐答案Python在缩进上运行所有内容.缩进级别是它如何知道随什么变化的方式.
Python runs everything on indentation. The indentation level is how it knows what goes with what.
例如,这有效:
for i in range(10): print i但是这会以IndentationError炸毁:
for i in range(10): print i来自文档:
逻辑开头的空白(空格和制表符) line用于计算该行的缩进级别,其中 turn用于确定语句分组.
Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.
更多推荐
for循环代码块
发布评论