W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
下面是你要寫又一個(gè)python腳本,這節(jié)練習(xí)會(huì)向你介紹“if語句”。把這段代碼輸入腳本,讓它正常運(yùn)行,看看你有沒有什么收獲。
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."
$ 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í)題前,試著自己回答下面的問題:
- 你認(rèn)為
if
對(duì)于它下一行的代碼做了什么?- 為什么
if
語句的下一行需要縮進(jìn)?- 如果不縮進(jìn),會(huì)怎樣?
- 把習(xí)題 27 中的其它布爾表達(dá)式放到
if語句
中能不能運(yùn)行呢?試一下。- 如果把變量
people
,cats
, 和dogs
的初始值改掉,會(huì)怎樣?
+=
表示什么意思?代碼
x += 1
和x = x + 1
實(shí)現(xiàn)的是一樣的功能,但是可以少輸入一些字符。你可以稱之為“增量”操作符。-=
也是相同的,后面你會(huì)看到更多的相關(guān)解釋。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: