《笨办法学Python3》练习二十九:if结构

练习代码

people = 20
cats = 30
dogs = 15

if people < cats:
    print("Too many cats! The world is doomed!")

if people > cats:
    print("Not many cats! The world is saved!")

if people < dogs:
    print("The world is drooled on!")

if people > dogs:
    print("The world is dry!")

dogs += 5

if people >= dogs:
    print("People are greater than or equal to dogs.")
if people <= dogs:
    print("People are less than or equal to dogs.")
if people == dogs:
    print("People are dogs.") # WTF?

Study Drills

  1. What do you think the if does to the code under it?

  2. Why does the code under the if need to be indented four spaces?

  3. What happens if it isn’t indented?

  4. Can you put other Boolean expressions from Exercise 27 in the if-statement? Try it.

  5. What happens if you change the initial values for people, cats, and dogs?

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,451评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,940评论 0 23
  • 近日热议的“理发”事件让教育者不得不警醒,为何这样说?因为纵观整件事,即班主任老师带学生理发——学生因头发被理不上...
    子阡陌阅读 1,158评论 0 7
  • 一个人的素养高低直接体现在日常的待人接物上,其人的一言一行就是他的素养。有的人彬彬有礼、有的人横眉冷对、有的...
    一抹涟漪阅读 1,202评论 1 1
  • 碧水蓝天 风光无限 兰亭内诗意翩翩 公元几年 梦里寻见 真迹恰似在眼前 笔落石砚 悲喜万千 一纸行书叹恩怨 彼岸美...
    萍水相逢yu阅读 353评论 3 8