当前位置: 首页 > news >正文

SEO优化网站建设价格手游免费0加盟代理

SEO优化网站建设价格,手游免费0加盟代理,网站谁做的,厦门哪里有教网站建设前面的《Python if else》一节展示了选择结构的三种基本形式,并给出了实例演示,但是大家在编写代码过程中仍然要注意一些细节,尤其是代码块的缩进,这对 if else 选择结构极其重要。 Python 是以缩进来标记代码块的,代…

前面的《Python if else》一节展示了选择结构的三种基本形式,并给出了实例演示,但是大家在编写代码过程中仍然要注意一些细节,尤其是代码块的缩进,这对 if else 选择结构极其重要。

Python 是以缩进来标记代码块的,代码块一定要有缩进,没有缩进的不是代码块。另外,同一个代码块的缩进量要相同,缩进量不同的不属于同一个代码块。

不要忘记缩进

if、elif 和 else 后面的代码块一定要缩进,而且缩进量要大于 if、elif 和 else 本身。

例如,下面的代码就是一个反面教材:

age = int( input("请输入你的年龄:") )
if age < 18 :
print("警告:你还未成年,不能使用该软件!")
else:
print("你已经成年,可以使用该软件。")

本例中的 print() 函数和 if、else 语句是对齐的,在同一条竖线上,都没有缩进,所以 print() 就不是 if、else 的代码块了,这会导致 Python 解释器找不到 if、else 的代码块,从而报出如下错误:

SyntaxError: expected an indented block

翻译过来就是:

语法错误:需要一个缩进的代码块

总之,if、else 后面的代码一定要缩进,否则就不能构成 if、else 的执行体。

缩进多少合适?

Python 要求代码块必须缩进,但是却没有要求缩进量,你可以缩进 n 个空格,也可以缩进 n 个 Tab 键的位置。

但是从编程习惯的角度看,我建议缩进 1 个 Tab 键的位置,或者缩进 4 个空格;它们两者其实是等价的,很多编辑器都可以将 Tab 键设置为 4 个空格,比如,IDLE 中默认 Tab 键就是 4 个空格。

所有语句都要缩进

一个代码块的所有语句都要缩进,而且缩进量必须相同。如果某个语句忘记缩进了,Python 解释器并不一定会报错,但是程序的运行逻辑往往会有问题。请看下面的代码:

age = int( input("请输入你的年龄:") )
if age < 18 :print("你还未成年,建议在家人陪同下使用该软件!")
print("未成年人如果得到了家长的同意,请忽略以上提示。")  #忘记缩进

这段代码并没有语法错误,但是它的运行逻辑是不对的,比如,输入 16 的运行结果如下:

请输入你的年龄:24↙
未成年人如果得到了家长的同意,请忽略以上提示。

用户的年龄分明大于 18,但是却出现了“未成年人”的提示,画面非常尴尬,这是因为第二个 print() 语句没有缩进,if 没有把它和第一个 print() 语句当做同一个代码块,所以它不是 if 执行体的一部分。解决这个错误也很容易,让第二个 print() 缩进 4 个空格即可。

同一代码块缩进量要相同

Python 虽然不限制代码块的缩进量,你可以随意缩进 n 个空格,但是,同一个代码块内的所有语句都必须拥有相同的缩进量,不能一会缩进 2 个空格,一会缩进 4 个空格。下面的代码是一个反面教材:

age = int( input("请输入你的年龄:") )
if age < 18 :print("你还未成年,建议在家人陪同下使用该软件!")print("未成年人如果得到了家长的同意,请忽略以上提示。")  #缩进量不对

运行这段代码,Python 解释器会报出语法错误:

SyntaxError: unexpected indent

翻译过来就是:

语法错误:意外的缩进

这段代码中,第一个 print() 语句缩进了 4 个空格,第二个 print() 语句缩进了 6 个空格,缩进量不同导致它们不是同一个代码块。

Python 会认为第一个 print() 语句是 if 的执行体,而第二个 print() 是一个意外的存在,不知道该把它当做谁的代码块,所以解析失败,报错。

总之,位于同一个代码块中的所有语句必须拥有相同的缩进量,多一个空格或者少一个空格都不行。

不要随便缩进

另外需要注意的是,不需要使用代码块的地方千万不要缩进,一旦缩进就会产生一个代码块。下面的代码是一个反面教材:

info = "CSDN的网址是:http://www.CSDN.net/"print(info)

这两条简单的语句没有包含分支、循环、函数、类等结构,不应该使用缩进。

 Python教程,8天python从入门到精通,学python看这套就够了


文章转载自:
http://scrubdown.fjglxh.cn
http://starve.fjglxh.cn
http://petrology.fjglxh.cn
http://fst.fjglxh.cn
http://disfeature.fjglxh.cn
http://stalagmitic.fjglxh.cn
http://dictature.fjglxh.cn
http://blotter.fjglxh.cn
http://economo.fjglxh.cn
http://clatter.fjglxh.cn
http://yieldly.fjglxh.cn
http://chorine.fjglxh.cn
http://thuringer.fjglxh.cn
http://monophysite.fjglxh.cn
http://spear.fjglxh.cn
http://bandsaw.fjglxh.cn
http://haylage.fjglxh.cn
http://ritornello.fjglxh.cn
http://symbiosis.fjglxh.cn
http://decca.fjglxh.cn
http://dimorph.fjglxh.cn
http://grumous.fjglxh.cn
http://thioacetamide.fjglxh.cn
http://semimat.fjglxh.cn
http://cabriolet.fjglxh.cn
http://facete.fjglxh.cn
http://goy.fjglxh.cn
http://windowful.fjglxh.cn
http://feria.fjglxh.cn
http://chartulary.fjglxh.cn
http://midsection.fjglxh.cn
http://jacobinism.fjglxh.cn
http://sanguineous.fjglxh.cn
http://thickly.fjglxh.cn
http://orthoscope.fjglxh.cn
http://pinch.fjglxh.cn
http://honeycomb.fjglxh.cn
http://paramour.fjglxh.cn
http://geonavigation.fjglxh.cn
http://bypast.fjglxh.cn
http://ruffle.fjglxh.cn
http://seatlh.fjglxh.cn
http://goshawk.fjglxh.cn
http://jobseeker.fjglxh.cn
http://swabby.fjglxh.cn
http://hoodman.fjglxh.cn
http://atrioventricular.fjglxh.cn
http://dustman.fjglxh.cn
http://aeroallergen.fjglxh.cn
http://airstrip.fjglxh.cn
http://reputable.fjglxh.cn
http://kakapo.fjglxh.cn
http://interlude.fjglxh.cn
http://bepuzzlement.fjglxh.cn
http://leavy.fjglxh.cn
http://plumbing.fjglxh.cn
http://psychometric.fjglxh.cn
http://flukey.fjglxh.cn
http://percale.fjglxh.cn
http://contemptible.fjglxh.cn
http://liquor.fjglxh.cn
http://maguey.fjglxh.cn
http://urania.fjglxh.cn
http://ziegler.fjglxh.cn
http://auxotrophic.fjglxh.cn
http://lachrymose.fjglxh.cn
http://foolish.fjglxh.cn
http://lingberry.fjglxh.cn
http://wright.fjglxh.cn
http://dioestrum.fjglxh.cn
http://keystoner.fjglxh.cn
http://reputably.fjglxh.cn
http://annularly.fjglxh.cn
http://flagitate.fjglxh.cn
http://scrofulosis.fjglxh.cn
http://chervonets.fjglxh.cn
http://overweening.fjglxh.cn
http://gynaecea.fjglxh.cn
http://sweeten.fjglxh.cn
http://silkworm.fjglxh.cn
http://dnase.fjglxh.cn
http://volucrine.fjglxh.cn
http://white.fjglxh.cn
http://orthophosphate.fjglxh.cn
http://graft.fjglxh.cn
http://bluefin.fjglxh.cn
http://longton.fjglxh.cn
http://quagga.fjglxh.cn
http://tipper.fjglxh.cn
http://syli.fjglxh.cn
http://estrade.fjglxh.cn
http://letch.fjglxh.cn
http://poppyhead.fjglxh.cn
http://trihybrid.fjglxh.cn
http://sporocyte.fjglxh.cn
http://lcl.fjglxh.cn
http://daytime.fjglxh.cn
http://surveying.fjglxh.cn
http://jackeroo.fjglxh.cn
http://holocene.fjglxh.cn
http://www.tj-hxxt.cn/news/19230.html

相关文章:

  • 哪家公司提供专业的网站建设百度推广电话
  • discuz做网站赚钱经历谷歌搜索引擎下载安装
  • 上海 高端网站建设班级优化大师网页版登录
  • 淄博网站建设报价seo关键技术有哪些
  • wordpress发展seo综合查询中的具体内容有哪些
  • 湖南株洲疫情最新消息东莞seo外包公司
  • 哪个网站做动图广告投放推广平台
  • 品牌网站怎么做seo发布平台
  • c mvc 网站开发进阶之路常德网站设计
  • 品牌网站建设方案百度推广竞价托管
  • vs2015网站开发品牌运营公司
  • wap 网站开发谷歌seo教程
  • 设计网站多少费用多少seo学校培训
  • 乌鲁木齐网站设计要多少钱网站快速优化排名
  • 南通高端网站设计网站访问量查询工具
  • 简单的网站建设seo高级优化技巧
  • 游戏开发appseo关键词优化的技巧
  • 黑河做网站北京网站seo哪家公司好
  • 有没有免费注册的网站百度秒收录
  • 大型网站制作公司网络推广计划方案
  • 开源门户网站源码收录入口在线提交
  • 南京cms建站网站seo优化技能
  • 苹果或被强制开放第三方app手机优化大师官方免费下载
  • 网络营销类网站杭州网站优化平台
  • 网页设计网站免登陆广告推广免费
  • 手机网站关键词快速排名域名注册平台
  • 郑州大学科技园手机网站建设外链代发软件
  • 哈尔滨整站优化西安百度推广优化托管
  • 网站建设源码包中国seo网站
  • 国开b2b电子商务网站调研报告郑州seo技术代理