for循环代码块

编程入门 行业动态 更新时间:2024-10-27 12:31:08
本文介绍了for循环代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读有关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循环代码块

本文发布于:2023-11-29 19:14:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!