电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

Py4Inf-03-ConditionalPy4Inf-03-Conditional

32页
  • 卖家[上传人]:海天
  • 文档编号:23993782
  • 上传时间:2017-10-22
  • 文档格式:PDF
  • 文档大小:757.60KB
  • / 32 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、Conditional ExecutionChapter 3Python for Informatics: Exploring IUnless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License.http:/creativecommons.org/licenses/by/3.0/.Copyright 2010- Charles R. SeveranceConditional StepsOutput:SmallerFinisProgram:x = 5if x 20: print Biggerprint Finisx = 5X 20 ?print Biggerprint FinisYesYesComparison OperatorsBoolean expressions ask a question and produce a Yes or No result which we use to control prog

      2、ram flowBoolean expressions using comparison operators evaluate to - True / False - Yes / NoComparison operators look at variables but do not change the variableshttp:/en.wikipedia.org/wiki/George_BooleRemember: “=” is used for assignment.Python Meaning= Greater than or Equal Greater than!= Not equalComparison Operatorsx = 5if x = 5 : print Equals 5if x 4 :print Greater than 4if x = 5 :print Greater than or Equal 5if x Preferences - Language Menu/Tab SettingsTextWrangler: TextWrangler - Preferen

      3、ces - Editor DefaultsPython cares a *lot* about how far line is indented. If you mix tabs and spaces, you may get “indentation errors” even if everything looks finePlease do this now while you are thinking about it so we can all stay sane.This will save you much unnecessary pain.x = 5if x 2 :print Bigger than 2print Still biggerprint Done with 2for i in range(5) :print iif i 2 : print Bigger than 2print Done with i, ix = 5if x 2 :# commentsprint Bigger than 2# dont matterprint Still bigger# but

      4、can confuse youprint Done with 2# if you dont line # them upincrease / maintain after if or fordecrease to indicate end of blockblank lines and comment lines ignoredMental begin/end squaresx = 5if x 2 :print Bigger than 2print Still biggerprint Done with 2for i in range(5) :print iif i 2 : print Bigger than 2print Done with i, ix = 5if x 2 :# commentsprint Bigger than 2# dont matterprint Still bigger# but can confuse youprint Done with 2# if you dont line # them upx 1print More than onex 1 :prin

      5、t More than oneif x 1print More than onex 1 :print More than oneif x 1print More than onex 1 :print More than oneif x 2print BiggeryesnoX = 4print Not biggerprint All DoneTwo-way using else :x = 4if x 2 :print Biggerelse :print Smallerprint All donex 2print BiggeryesnoX = 4print Smallerprint All DoneTwo-way using else :x = 4if x 2 :print Biggerelse :print Smallerprint All donex 2print BiggeryesnoX = 4print Smallerprint All DoneMulti-wayif x = 2 : print Two or moreelse :print Something elseWhich

      6、will never print?The try / except StructureYou surround a dangerous section of code with try and except.If the code in the try works - the except is skippedIf the code in the try fails - it jumps to the except section$ cat notry.py astr = Hello Bobistr = int(astr)print First, istrastr = 123istr = int(astr)print Second, istr$ python notry.py Traceback (most recent call last):File notry.py, line 2, in istr = int(astr)ValueError: invalid literal for int() with base 10: Hello BobThe program stops he

      7、reAllDone SoftwareInputDevicesCentralProcessingUnitMainMemoryOutputDevicesSecondaryMemoryGenericComputer$ cat tryexcept.py astr = Hello Bobtry:istr = int(astr)except:istr = -1print First, istrastr = 123try:istr = int(astr)except:istr = -1print Second, istr$ python tryexcept.py First -1Second 123When the first conversion fails - it just drops into the except: clause and the program continues.When the second conversion succeeds - it just skips the except: clause and the program continues.try / exc

      8、eptastr = Bobastr = Bobtry:print Helloistr = int(astr)print Thereexcept:istr = -1print Done, istrprint Helloprint Thereistr = int(astr)print Done, istristr = -1Safety netSample try / except$ python trynum.py Enter a number:42Nice work$ python trynum.pyEnter a number:fourtytwoNot a number$rawstr = raw_input(Enter a number:)try: ival = int(rawstr)except: ival = -1if ival 0 : print Nice workelse: print Not a numberExerciseRewrite your pay computation to give the employee 1.5 times the hourly rate f

      9、or hours worked above 40 hours.Enter Hours: 45Enter Rate: 10 Pay: 475.0475 = 40 * 10 + 5 * 15ExerciseRewrite your pay program using try and except so that your program handles non-numeric input gracefully.Enter Hours: 20 Enter Rate: nine Error, please enter numeric inputEnter Hours: forty Error, please enter numeric inputSummaryComparison operators = = !=Logical operators: and or notIndentationOne Way DecisionsTwo way Decisions if : and else :Nested DecisionsMultiway decisions using elifTry / Except to compensate for errorsShort circuit evaluations

      《Py4Inf-03-ConditionalPy4Inf-03-Conditional》由会员海天分享,可在线阅读,更多相关《Py4Inf-03-ConditionalPy4Inf-03-Conditional》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    监控施工 信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.