create account

[DA series - Learn Python with Steem #03] 邏輯判斷 by deanliu

View this thread on: hive.blogpeakd.comecency.com
· @deanliu · (edited)
$25.45
[DA series - Learn Python with Steem #03] 邏輯判斷
[[DA series - Learn Python with Steem]](https://steemit.com/da-learnpythonwithsteem/@deanliu/da-series-learn-python-with-steem-00-coding) 是DA(@deanliu & @antonsteemit)關於「從Python程式語言實做Steem區塊鏈的入門」的系列,歡迎趕緊入列學習!

前情提要:[[DA series - Learn Python with Steem #02] 變數與資料型態](https://steemit.com/da-learnpythonwithsteem/@deanliu/da-series-learn-python-with-steem-02)

https://steemitimages.com/0x0/https://cdn.steemitimages.com/DQmbeN1PpqQC2JE5HbpXatb3apUfFhe68fPLctT95FiiRHq/cover.png

新的一週開始了!讓我們繼續學習吧!

第#03堂課,今天我們來看一個相對簡單但非常重要的主題:**邏輯判斷** 吧!

## Python的 If Else 邏輯判斷

邏輯判斷可以說是程式語言最古老的基本成份,也是程式中非常常出現的語法。看懂了if else之後,加上我們之前對於變數的理解,看一個陌生的簡易程式碼已經可以理解五六成了。

其實我相信很多人看到If else這些英文字,就已經可以猜到大概的意義了。沒錯,正如他們的中文翻譯,`if`就是「如果」的意思:**如果符合這個情況(邏輯判斷)就執行下列程式**;而`else`則是跟隨在`if `後面的,代表不符合`if`的其他情況下,就執行這些程式。所以整個if else,可以翻譯成中文的「如果...否則...」

我現在舉一個非常簡單的例子,懂了之後再來看看一些比較細項的規定:
![](https://cdn.steemitimages.com/DQmTg97YpSunshVfTvQM9TGnkkke55LFbN6J3nT1Dp3qcMg/image.png)

執行程式:`python tutorial_3.py`
Output: `You are not overweight!`

注:本篇教學的程式碼可至[GitHub/tutorial_3.py](https://github.com/antoncoding/Python-x-Steem-tutorial/blob/master/tutorial_3.py)中參考或下載。

在Python語言中,所有的**判斷式**都是用冒號(`:`)並且加上**換行縮排**來表示邏輯的上下關係。由上面的程式碼中可以看出,縮排的(按tab,相當於在段落前增加一個大空格)的 `print('You are not overweighted!')`是屬於 `if`的範圍,因此在 `if`成立時會被執行。
而`else`會跟`if`在同樣的水平位置,在`else:`的下一行也是一排經過縮排的內容,表示**else**成立的話,要執行的範圍。

### 關於判斷式
放在 `if`後面的 `BMI>24`是一個 **Expression**,也就是一個會回傳「True or False」的式子。這裡的「大於小於」判斷很好理解,如果正確就回傳`True`、錯誤就回傳`False`。比較要注意的事,如果要判斷相等的話要使用`==`兩個等號,因為一個等號在程式中是`賦值`的意思,兩個等號才是一個會回傳True or False的Expression。


你可能會想說,上次學到的**布林值**不是也是`True` or `False`嗎?沒錯!所以可以在`if` 之類的地方放上以前學過的布林值。例如:
```
is_married = True
if is_married:
	print('You are married')
else:
	print('blah blah blah')
```

## 關於縮排 
這些縮排的規定是非常重要的,因為python中沒有使用任何括號來把if 的範圍刮起來,python完全是透過換行以及縮排來判斷要怎麼執行程式。如果你的換行與縮排有錯誤的話,會讓程式不知道何者屬於`if`的範圍,或何者屬於`else`的範圍。例如下面就是一個錯誤例子:
![](https://cdn.steemitimages.com/DQmcgTCmPYsmNXN74mFALiN2qfJfosLhD8JU22YMmsu2PWk/image.png)

錯誤代碼如下:
```
  File "tutorial_3.py", line 7
    else:
       ^
SyntaxError: invalid syntax

```
我們可以來練習怎麼讀這些錯誤。他告訴我們是在第七行的`else`出了問題,而他會跳出錯誤正是因為所有`else`出現前,都應該先有個**同縮排等級的if**。不然沒有「如果」哪來的「否則」阿?


如果你在執行程式的時候發現`IndentationError`的錯誤,很可能是因為每次縮排的方式不同。在python中,縮排不只水平距離看起來要一樣,若是用空白鍵按很多次按出來的,跟使用`tab`鍵一次跳一大步跳出來的,儘管肉眼看起來好像一樣,在程式編譯(執行前準備)也是會被擋下來的。


## 多重判斷
如果只有`if `跟`else`的話,我們要撰寫一個判斷BMI是否正常的程式需要「兩層的if-else」。**因為在確定沒有過胖的情況下,還要在透過一次if - else 確定也沒有過瘦**,才算正常。因此我們的完整邏輯判斷會變成:

![](https://cdn.steemitimages.com/DQmdgimkYyjsgoKarenLA3okbZgVKKJLZ8jXTuwMAdfmnme/image.png)

執行結果:`You are just fit!`

不過這種多重判斷是很常遇到的,我們通常會有很多的這種非類的情況,例如:假設我們要區分一個數字是位於0~10, 11~20, 21~30, 31~40 哪一個區間,如果只用單純的`if - else`的話,就需要**四層判斷**。當然我們的python不會讓我們這麼累啦!所以就發明了 `elif`的語法,意思就是 **Else if**,中文可以翻譯為「若不是,那如果...」

例如上面的BMI例子,就可以透過`elif`改寫如下:
![](https://cdn.steemitimages.com/DQmQQ2MXjBrFZp6MhfgkoRhXsxdBoNZD4cm8RSrDip1FKwx/image.png)

所以這個程式會先判斷我的BMI是否大於24,若是,則輸出「You are overweight!」。
**若不是,那如果** BMI < 18,就輸出「You are underweight」。而最後剩餘的情況,也就是 BMI介於18~24之間,就是屬於正常。這樣寫是不是很簡單阿!

### 讓程式變有趣的小撇步
今天最後來教大家一個很好用的功能:讀取使用者輸入的函式:`input()`。
我們剛剛那些變數:`my_weight`、`my_height`都是在程式碼內自行輸入指定的,每次要測試不同的身高體重就要重新更改程式碼,要是可以在執行的時候手動輸入想要查詢的值就好了!因此有了`input()`這樣一個方便好用的函式。使用方式如下:

![](https://cdn.steemitimages.com/DQmXnHcMWj9xrsq3ev2zedmCUEraZAuTPstD1uFEtkYnUS7/image.png)

`input()`裡面放的字串是我們想要顯示在command line上的提示字元,在這裡就是詢問使用者,你的身高及體重為何。輸入完成後,這兩個值分別被存到`my_height`以及`my_weight`裡面。
但要注意的是,`input()`所讀進來的變數**型態為字串**,還記得我們前面說過,字串的加減乘除跟數字是不一樣的,其中乘法就會出錯。因此我們在使用這兩個變數計算BMI前,**要把他們的資料型態由「字串」轉為「數字」**。Python中我們可以直接用`float(x)`把一個x轉成 「有小數點的數字」,或是用`int(x)`把x轉成「正整數」。我們這裡兩個都是可以輸入小數點的,因此選擇用`float()`把讀到的字串轉成 **float**(這種有小數點的數字稱為**浮點數**)

執行結果如下:
![](https://cdn.steemitimages.com/DQmWd9mDrvMqfgDAeimTwUcmivHEoJzu5ix14WJNUsW4ssp/image.png)

是不是程式都變好玩了呀!

如你所見,`input()`可以幫助我們設計很多跟使用者互動的界面,大家也可以試著自己做做看一些簡單的「輸入並判斷」程式囉!

## Homework time:練習題!
從這章節開始,我們都在文末來指派個小小練習題好啦!讓大家自己建立一點成就感。今天小小的練習題:

利用If-Else Statement,設計一個程式 **要求使用者輸入三個數字,最後印出最大值:**

範例輸入與輸出
![](https://cdn.steemitimages.com/DQmPwgM7RoWzqWRcNxEE32TwFqe9qUio184CarpnwFUE9qK/image.png)

參考解答:[GitHub/@antoncoding](https://github.com/antoncoding/Python-x-Steem-tutorial/blob/master/tutorial_3_exercise.py)


我們下篇文章再見囉~下課!

![class-377117_1280.jpg](https://cdn.steemitimages.com/DQmbTcEoAB7HRTxy11a1eDN8vREiGqxEMd7P3jJ3TGvGSTv/class-377117_1280.jpg)
<sub>*image - pixabay*</sub>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 94 others
properties (23)
authordeanliu
permlinkda-series-learn-python-with-steem-03
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem","python","steem","cn-programming","cn"],"users":["deanliu","antonsteemit"],"image":["https://steemitimages.com/0x0/https://cdn.steemitimages.com/DQmbeN1PpqQC2JE5HbpXatb3apUfFhe68fPLctT95FiiRHq/cover.png","https://cdn.steemitimages.com/DQmTg97YpSunshVfTvQM9TGnkkke55LFbN6J3nT1Dp3qcMg/image.png","https://cdn.steemitimages.com/DQmcgTCmPYsmNXN74mFALiN2qfJfosLhD8JU22YMmsu2PWk/image.png","https://cdn.steemitimages.com/DQmdgimkYyjsgoKarenLA3okbZgVKKJLZ8jXTuwMAdfmnme/image.png","https://cdn.steemitimages.com/DQmQQ2MXjBrFZp6MhfgkoRhXsxdBoNZD4cm8RSrDip1FKwx/image.png","https://cdn.steemitimages.com/DQmXnHcMWj9xrsq3ev2zedmCUEraZAuTPstD1uFEtkYnUS7/image.png","https://cdn.steemitimages.com/DQmWd9mDrvMqfgDAeimTwUcmivHEoJzu5ix14WJNUsW4ssp/image.png","https://cdn.steemitimages.com/DQmPwgM7RoWzqWRcNxEE32TwFqe9qUio184CarpnwFUE9qK/image.png","https://cdn.steemitimages.com/DQmbTcEoAB7HRTxy11a1eDN8vREiGqxEMd7P3jJ3TGvGSTv/class-377117_1280.jpg"],"links":["https://steemit.com/da-learnpythonwithsteem/@deanliu/da-series-learn-python-with-steem-00-coding","https://steemit.com/da-learnpythonwithsteem/@deanliu/da-series-learn-python-with-steem-02","https://github.com/antoncoding/Python-x-Steem-tutorial/blob/master/tutorial_3.py","https://github.com/antoncoding/Python-x-Steem-tutorial/blob/master/tutorial_3_exercise.py"],"app":"steemit/0.1","format":"markdown"}
created2018-07-30 02:13:48
last_update2018-07-31 08:12:39
depth0
children35
last_payout2018-08-06 02:13:48
cashout_time1969-12-31 23:59:59
total_payout_value21.673 HBD
curator_payout_value3.777 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,339
author_reputation3,088,559,687,127,212
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,455,282
net_rshares14,889,142,481,308
author_curate_reward""
vote details (158)
@aaronli ·
python 有沒有 **case** 的
properties (22)
authoraaronli
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180731t054138085z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-31 05:41:39
last_update2018-07-31 05:41:39
depth1
children0
last_payout2018-08-07 05:41:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation346,133,855,586,244
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,598,773
net_rshares0
@ace108 ·
FYI, the code
![](https://cdn.steemitimages.com/DQmen3GPbSsgvhhrAEvkwu1zd5yddGKjej3g72ckER7DMuZ/image.png)
Doesn't sync with 
Output: You are not overweight<strong>ed!></strong>
properties (22)
authorace108
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t140520898z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"image":["https://cdn.steemitimages.com/DQmen3GPbSsgvhhrAEvkwu1zd5yddGKjej3g72ckER7DMuZ/image.png"],"app":"steemit/0.1"}
created2018-07-30 14:05:21
last_update2018-07-30 14:05:21
depth1
children0
last_payout2018-08-06 14:05:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length177
author_reputation1,221,077,086,915,775
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,518,190
net_rshares0
@joythewanderer ·
我要赶紧跟上你们的步伐,先去下载Python lol
properties (22)
authorjoythewanderer
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t081232912z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 08:12:48
last_update2018-07-30 08:12:48
depth1
children2
last_payout2018-08-06 08:12:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length26
author_reputation1,916,082,145,948,706
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,484,762
net_rshares0
@deanliu ·
走很慢的~~ ^_^
properties (22)
authordeanliu
permlinkre-joythewanderer-re-deanliu-da-series-learn-python-with-steem-03-20180730t104051959z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 10:40:51
last_update2018-07-30 10:40:51
depth2
children0
last_payout2018-08-06 10:40:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation3,088,559,687,127,212
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,497,713
net_rshares0
@shine.wong ·
你很快能赶上的!

Posted using [Partiko Android](https://play.google.com/store/apps/details?id=io.partiko.android)
properties (22)
authorshine.wong
permlinkshine-wong-re-joythewanderer-re-deanliu-da-series-learn-python-with-steem-03-20180730t104819029z
categoryda-learnpythonwithsteem
json_metadata{"app":"partiko"}
created2018-07-30 10:48:18
last_update2018-07-30 10:48:18
depth2
children0
last_payout2018-08-06 10:48:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length106
author_reputation1,329,335,530,903
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,498,329
net_rshares0
@kimxinfo ·
$0.13
![py-3.png](https://ipfs.busy.org/ipfs/QmTxEsYLx4X1Y7BJPvKeFT6j4FPj5h1Ft3hqvKwSBNiZAA)
👍  
properties (23)
authorkimxinfo
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t033416403z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":["https://ipfs.busy.org/ipfs/QmTxEsYLx4X1Y7BJPvKeFT6j4FPj5h1Ft3hqvKwSBNiZAA"]}
created2018-07-30 03:34:15
last_update2018-07-30 03:34:15
depth1
children5
last_payout2018-08-06 03:34:15
cashout_time1969-12-31 23:59:59
total_payout_value0.096 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length87
author_reputation1,748,728,029,640
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,461,458
net_rshares75,128,514,493
author_curate_reward""
vote details (1)
@kimxinfo ·
丫...下次注意取名XD
properties (22)
authorkimxinfo
permlinkre-kimxinfo-re-deanliu-da-series-learn-python-with-steem-03-20180730t053154354z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":[]}
created2018-07-30 05:31:54
last_update2018-07-30 05:31:54
depth2
children0
last_payout2018-08-06 05:31:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation1,748,728,029,640
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,470,794
net_rshares0
@yjcps ·
好奇怪,为什么你的max变量的颜色与其他的变量颜色不一样?
properties (22)
authoryjcps
permlinkre-kimxinfo-re-deanliu-da-series-learn-python-with-steem-03-20180730t035639508z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":[]}
created2018-07-30 03:56:42
last_update2018-07-30 03:56:42
depth2
children3
last_payout2018-08-06 03:56:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length29
author_reputation1,019,613,572,923
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,463,177
net_rshares0
@antonsteemit ·
$0.05
你的寫法是沒問題的,只是因為max剛好是python一個常用function的名字,所以你的編輯器會自動幫忙變色。不過現在你宣告這個max 等於一個變數了,所以他其實已經被你覆蓋掉了~沒問題的。
👍  ,
properties (23)
authorantonsteemit
permlinkre-yjcps-re-kimxinfo-re-deanliu-da-series-learn-python-with-steem-03-20180730t044556693z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":[]}
created2018-07-30 04:45:57
last_update2018-07-30 04:45:57
depth3
children0
last_payout2018-08-06 04:45:57
cashout_time1969-12-31 23:59:59
total_payout_value0.043 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length97
author_reputation7,534,465,964,895
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,467,027
net_rshares33,489,250,327
author_curate_reward""
vote details (2)
@shine.wong · (edited)
$0.04
![image.png](https://files.steempeak.com/file/steempeak/shine.wong/dtuLU9I5-image.png)
因为这个max不是变量
👍  
properties (23)
authorshine.wong
permlinkre-yjcps-re-kimxinfo-re-deanliu-da-series-learn-python-with-steem-03-20180730t035856757z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"community":"steempeak","app":"steempeak"}
created2018-07-30 03:58:57
last_update2018-07-30 04:03:45
depth3
children1
last_payout2018-08-06 03:58:57
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length98
author_reputation1,329,335,530,903
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,463,358
net_rshares25,042,838,164
author_curate_reward""
vote details (1)
@kona ·
$0.13
my hw:
![hw3.png](https://cdn.steemitimages.com/DQmdfGqVZvzmbeeKVRTR1pEh8FtYHHztGVdUvA3JaFK1E8g/hw3.png)
👍  
properties (23)
authorkona
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t040055295z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"image":["https://cdn.steemitimages.com/DQmdfGqVZvzmbeeKVRTR1pEh8FtYHHztGVdUvA3JaFK1E8g/hw3.png"],"app":"steemit/0.1"}
created2018-07-30 04:00:57
last_update2018-07-30 04:00:57
depth1
children11
last_payout2018-08-06 04:00:57
cashout_time1969-12-31 23:59:59
total_payout_value0.096 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length104
author_reputation39,758,138,507,499
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,463,511
net_rshares75,128,514,493
author_curate_reward""
vote details (1)
@kona ·
有個問題,為什麼我在line6, 如果不打東西會顯示IndentationError,可是這樣加上print(''),好像有點多餘,求解答!
properties (22)
authorkona
permlinkre-kona-re-deanliu-da-series-learn-python-with-steem-03-20180730t040612914z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 04:06:15
last_update2018-07-30 04:06:15
depth2
children10
last_payout2018-08-06 04:06:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length70
author_reputation39,758,138,507,499
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,463,946
net_rshares0
@antonsteemit ·
對,聽高手@yjcps的!
properties (22)
authorantonsteemit
permlinkre-kona-re-kona-re-deanliu-da-series-learn-python-with-steem-03-20180730t044337044z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":["yjcps"],"links":["/@yjcps"],"image":[]}
created2018-07-30 04:43:36
last_update2018-07-30 04:43:36
depth3
children1
last_payout2018-08-06 04:43:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation7,534,465,964,895
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,466,873
net_rshares0
@kona · (edited)
謝謝各高手,學了pass,
還想問問,沒看清題目,這樣用上elif好還是只用if..else好?
properties (22)
authorkona
permlinkre-kona-re-kona-re-deanliu-da-series-learn-python-with-steem-03-20180730t052757212z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 05:27:57
last_update2018-07-30 05:28:48
depth3
children2
last_payout2018-08-06 05:27:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length48
author_reputation39,758,138,507,499
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,470,466
net_rshares0
@shine.wong ·
$0.04
你可以吧这个print换成pass
![](https://cdn.steemitimages.com/DQmQNGcPgkeWosdePieCnKYLTwVxUjraNeUKuPt3ebiAR1d/image.png)
这样符合语法,但是你这个if里就不要写其他的了,因为我不知道pass是什么作用,只是加了后语法正常能够过编译,如果你加了其他东西,那所加的内容还是会正常运行的。
👍  
properties (23)
authorshine.wong
permlinkre-kona-re-kona-re-deanliu-da-series-learn-python-with-steem-03-20180730t044907871z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"image":["https://cdn.steemitimages.com/DQmQNGcPgkeWosdePieCnKYLTwVxUjraNeUKuPt3ebiAR1d/image.png"],"app":"steemit/0.1"}
created2018-07-30 04:49:09
last_update2018-07-30 04:49:09
depth3
children0
last_payout2018-08-06 04:49:09
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length191
author_reputation1,329,335,530,903
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,467,257
net_rshares25,042,838,164
author_curate_reward""
vote details (1)
@yjcps ·
$0.04
应该是不写东西不符合Python的语法。
在 if True: 的后面要写做出选择后要做的事情,什么都不做写个pass,像这样:

``` python
if True:
    pass
```
👍  ,
properties (23)
authoryjcps
permlinkre-kona-re-kona-re-deanliu-da-series-learn-python-with-steem-03-20180730t044144827z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":[]}
created2018-07-30 04:41:45
last_update2018-07-30 04:41:45
depth3
children3
last_payout2018-08-06 04:41:45
cashout_time1969-12-31 23:59:59
total_payout_value0.033 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length98
author_reputation1,019,613,572,923
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,466,727
net_rshares25,198,253,471
author_curate_reward""
vote details (2)
@plokmi ·
太好了,最近正好也在自学着Python!感谢你的分享!
👍  ,
properties (23)
authorplokmi
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t065004968z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 06:50:03
last_update2018-07-30 06:50:03
depth1
children1
last_payout2018-08-06 06:50:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation8,641,481,688,056
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,477,448
net_rshares366,359,273
author_curate_reward""
vote details (2)
@deanliu ·
不用客氣。
properties (22)
authordeanliu
permlinkre-plokmi-re-deanliu-da-series-learn-python-with-steem-03-20180730t104125447z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 10:41:24
last_update2018-07-30 10:41:24
depth2
children0
last_payout2018-08-06 10:41:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5
author_reputation3,088,559,687,127,212
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,497,756
net_rshares0
@sdream · (edited)
$0.13
收益了。谢谢!

顺便提个小小的建议:博文尾部加一条回索到上一课的博文链接就更好。
👍  
properties (23)
authorsdream
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t021721372z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 02:17:24
last_update2018-07-30 02:19:57
depth1
children2
last_payout2018-08-06 02:17:24
cashout_time1969-12-31 23:59:59
total_payout_value0.115 HBD
curator_payout_value0.011 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length41
author_reputation591,097,764,011
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id66,455,542
net_rshares75,128,514,493
author_curate_reward""
vote details (1)
@deanliu ·
good idea! thanks!
properties (22)
authordeanliu
permlinkre-sdream-re-deanliu-da-series-learn-python-with-steem-03-20180730t022952587z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 02:29:51
last_update2018-07-30 02:29:51
depth2
children1
last_payout2018-08-06 02:29:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length18
author_reputation3,088,559,687,127,212
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,456,503
net_rshares0
@sdream ·
更谢谢你的礼包!
properties (22)
authorsdream
permlinkre-deanliu-re-sdream-re-deanliu-da-series-learn-python-with-steem-03-20180730t023158708z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":[]}
created2018-07-30 02:32:00
last_update2018-07-30 02:32:00
depth3
children0
last_payout2018-08-06 02:32:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8
author_reputation591,097,764,011
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,456,665
net_rshares0
@shine.wong · (edited)
$0.13
![image.png](https://files.steempeak.com/file/steempeak/shine.wong/RMjkCNhL-image.png)
交作业,我还担心负数会有什么问题呐,结果好像没有,但是这个任务的要求貌似我没用到elif,希望下次用吧,哈哈哈~
👍  
properties (23)
authorshine.wong
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t035746717z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"community":"steempeak","app":"steempeak"}
created2018-07-30 03:57:48
last_update2018-07-30 04:00:12
depth1
children0
last_payout2018-08-06 03:57:48
cashout_time1969-12-31 23:59:59
total_payout_value0.096 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length143
author_reputation1,329,335,530,903
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,463,279
net_rshares75,128,514,493
author_curate_reward""
vote details (1)
@shine.wong ·
$0.13
看看回复,我又发现了这个用法~
![image.png](https://files.steempeak.com/file/steempeak/shine.wong/w3GSLqD0-image.png)
👍  ,
properties (23)
authorshine.wong
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t040706039z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"community":"steempeak","app":"steempeak"}
created2018-07-30 04:07:06
last_update2018-07-30 04:07:06
depth1
children2
last_payout2018-08-06 04:07:06
cashout_time1969-12-31 23:59:59
total_payout_value0.096 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length102
author_reputation1,329,335,530,903
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,464,021
net_rshares75,128,514,493
author_curate_reward""
vote details (2)
@yjcps ·
发现新大陆啊,哈哈
👍  
properties (23)
authoryjcps
permlinkre-shinewong-re-deanliu-da-series-learn-python-with-steem-03-20180730t044221471z
categoryda-learnpythonwithsteem
json_metadata{"community":"busy","app":"busy/2.5.3","format":"markdown","tags":["da-learnpythonwithsteem"],"users":[],"links":[],"image":[]}
created2018-07-30 04:42:21
last_update2018-07-30 04:42:21
depth2
children1
last_payout2018-08-06 04:42:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation1,019,613,572,923
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,466,792
net_rshares152,177,488
author_curate_reward""
vote details (1)
@shine.wong ·
多谢你的提点和提问!
properties (22)
authorshine.wong
permlinkre-yjcps-re-shinewong-re-deanliu-da-series-learn-python-with-steem-03-20180730t045014152z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 04:50:15
last_update2018-07-30 04:50:15
depth3
children0
last_payout2018-08-06 04:50:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation1,329,335,530,903
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,467,344
net_rshares0
@yingpingzhang ·
了解一下!
properties (22)
authoryingpingzhang
permlinkre-deanliu-da-series-learn-python-with-steem-03-20180730t034549408z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 03:45:48
last_update2018-07-30 03:45:48
depth1
children2
last_payout2018-08-06 03:45:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5
author_reputation1,348,872,191,969
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,462,309
net_rshares0
@deanliu ·
碼神你別裝了啦!XD
properties (22)
authordeanliu
permlinkre-yingpingzhang-re-deanliu-da-series-learn-python-with-steem-03-20180730t103750763z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-07-30 10:37:48
last_update2018-07-30 10:37:48
depth2
children1
last_payout2018-08-06 10:37:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation3,088,559,687,127,212
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,497,467
net_rshares0
@yingpingzhang ·
python我确实不懂啊!
properties (22)
authoryingpingzhang
permlinkre-deanliu-re-yingpingzhang-re-deanliu-da-series-learn-python-with-steem-03-20180820t130011873z
categoryda-learnpythonwithsteem
json_metadata{"tags":["da-learnpythonwithsteem"],"app":"steemit/0.1"}
created2018-08-20 13:00:15
last_update2018-08-20 13:00:15
depth3
children0
last_payout2018-08-27 13:00:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation1,348,872,191,969
root_title"[DA series - Learn Python with Steem #03] 邏輯判斷"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,801,373
net_rshares0