python - Python缩进不清楚
问题描述
试图了解缩进在 Python 中是如何工作的。
var = 100
if ( var == 100 ) :
print ("Value of expression is 100")
print ("Good bye!")
print("AM I out?")
输出:
Value of expression is 100
Good bye!
AM I out?
考虑到 if 条件给出的缩进,它不应该只打印下面的输出吗?
Value of expression is 100
Good bye!
为什么最后一个语句也打印出来,即使它没有像前面的语句那样缩进?
解决方案
Python 逐行执行你的程序,
var = 100 # First assign var = 100
if ( var == 100 ) : # Executed your if condition
print ("Value of expression is 100")
print ("Good bye!")
print("AM I out?") # Executed your last line
如果您不想在使用 if 时执行最后一行,请保留print("AM I out?")
在 else 块中。
if ( var == 100 ) :
print ("Value of expression is 100")
print ("Good bye!")
else:
print("AM I out?")
关于识别:
它用于了解您的程序,特定的块语句属于特定的对象(类、函数、if 条件、循环)
推荐阅读
- php - 在 Wordpress 中显示分类法
- .net-core - MSBUILD:错误 MSB1009:在 SonarQube 服务器上构建项目时项目文件不存在
- zabbix - Zabbix“触发器概述”升级到 v5.0.3 后为空
- javascript - 动作文本(Trix)样式不会在生产中加载(Rails 6,Heroku)
- python - gee 'sampleRectangle()' 返回 1x1 数组
- gremlin - Gremlin-python:如何编写在 where 条件下使用两个连接顶点的属性的查询?
- python - Geopandas:AttributeError:“事务”对象没有属性“run_callable”
- python - 如何使用 Oracle PL/SQL 块并通过 Python 获取结果
- asp.net - ASP.NET VB 代码隐藏 - 错误服务器响应为:5.7.1 客户端未通过身份验证
- python-3.x - 在字典中添加字典