Re: [閒聊] 不使用if的寫法

看板Programming作者 (saplayer)時間12年前 (2013/01/07 13:29), 編輯推噓0(004)
留言4則, 3人參與, 最新討論串4/18 (看更多)
python: i = 1 print(i==1 and "yes" or "no") ※ 引述《stanwu (stanwu)》之銘言: : Hi 各位大大 : 這應該算是骨灰級的老問題了 : int i = 1; : if ( i == 1 ) : printf ("yes"); : else : printf ("no"); : PS: 可彈性調整 i 裡面的值 0-65535 : 如果改寫成 完全拋棄不使用 if 或是 ? : 之類的判斷式, 有其他寫法可以得到一樣的結果嗎? : 任何語言都可以, 感謝各位大大 ^O^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.28.171 ※ 編輯: saplayer 來自: 140.112.28.171 (01/07 21:43)

01/08 09:56, , 1F
Python 這個比較犯規, 他的 ?: 就是 and or
01/08 09:56, 1F

01/08 10:01, , 2F
and的結果是取那個物件的值而不是ture/false
01/08 10:01, 2F

01/08 10:01, , 3F
這真是蠻混淆的
01/08 10:01, 3F

01/09 13:29, , 4F
一般會這樣寫"yes" if i == 1 else "no"
01/09 13:29, 4F
文章代碼(AID): #1GwitB2m (Programming)
討論串 (同標題文章)
文章代碼(AID): #1GwitB2m (Programming)