鍍金池/ 教程/ Python/ exercise29. IF 語句
附錄 A-練習(xí) 9:生成一個空文件(Touch, New-Item)
附錄 A-練習(xí) 10:復(fù)制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習(xí) 14:刪除文件 (rm)
附錄 A-練習(xí) 11:移動文件 (mv)
exercise46.項目骨架
附錄 A-練習(xí) 3:如果你迷路了
exercise37.復(fù)習(xí)符號
exercise47.自動化測試
exercise3.數(shù)字和數(shù)學(xué)計算
附錄 A-練習(xí) 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對象、類、以及從屬關(guān)系
exercise48.更復(fù)雜的用戶輸入
下一步
簡介
附錄 A-練習(xí) 7:刪除路徑 (rmdir)
exercise49.寫代碼語句
exercise18.命名, 變量, 代碼, 函數(shù)
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對象
附錄 A-練習(xí) 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數(shù), 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達式
附錄 A-練習(xí) 4:創(chuàng)建一個路徑 (mkdir)
附錄 A-練習(xí) 15:退出命令行 (exit)
exercise25. 更多更多的練習(xí)
exercise6.字符串和文本
exercise2.注釋和井號“#”
exercise21. 函數(shù)的返回值
附錄 A-下一步
exercise1.第一個程序
exercise23. 閱讀代碼
附錄 A-練習(xí) 5:改變當(dāng)前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習(xí)
exercise19.函數(shù)和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學(xué)到了什么?
exercise41.學(xué)會說面向?qū)ο?/span>
exercise52.開始你的 web 游戲
exercise20. 函數(shù)和文件
exercise15.讀文件
exercise45.你來制作一個游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數(shù)
exercise26. 恭喜你,可以進行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語句
exercise36.設(shè)計和調(diào)試
exercise0.安裝和準(zhǔn)備
exercise50.你的第一個網(wǎng)站
附錄 A-練習(xí) 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習(xí) 6:列出當(dāng)前路徑 (ls)
exercise16.讀寫文件
exercise4.變量和命名
exercise34.訪問列表元素
exercise11.提問
exercise43.基本的面向?qū)ο蟮姆治龊驮O(shè)計
附錄 A-簡介
附錄 A-練習(xí) 8:目錄切換(pushd, popd)
來自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習(xí) 13:輸出文件 (cat)
exercise39.字典,可愛的字典

exercise29. IF 語句

下面是你要寫又一個 python 腳本,這節(jié)練習(xí)會向你介紹“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."

你看到的結(jié)果

$ python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

附加題

猜猜“if 語句”是什么,它有什么用處。在做下一道習(xí)題前,試著自己回答下面的問題:

1.你認為 if 對于它下一行的代碼做了什么? 2.為什么 if 語句的下一行需要縮進? 3.如果不縮進,會怎樣? 4.把習(xí)題 27 中的其它布爾表達式放到 if 語句中能不能運行呢?試一下。 5.如果把變量 people, cats, 和 dogs 的初始值改掉,會怎樣?

常見問題

Q: +=表示什么意思?

代碼 x += 1 和 x = x + 1 實現(xiàn)的是一樣的功能,但是可以少輸入一些字符。你可以稱之為“增量”操作符。-= 也是相同的,后面你會看到更多的相關(guān)解釋。