看板 Python 關於我們 聯絡資訊
※ 引述《nanokevin (明日幸福今日修)》之銘言: : 大家好 : 小弟在練習一個猜數字的問題 : 其中一段程式如下 : res = input("Enter 'h' to indicate the guess is too high. Enter 'l' to : indicate the guess is too low. Enter 'c' to indicate I guessed correctly.") : while (res is 'c')==False: : If res=='h': : 請用戶輸入 h 或 l 或 c : 然後 用戶輸入的str 會放入res : 我的疑問是下面的invalid syntax該如何修正呢 (我不知道問題在哪) : If res=='h': : ^ : SyntaxError: invalid syntax : 此關於如果輸入不為c : 我的寫法是 while (res is 'c')==False: : 我好奇有其他的寫法嗎? : 謝謝 1. 布林判斷(True、False)不需要用到「==」或「is」。 2. 判斷字串相等之類的不要用「is」,請用「==」或「!=」。 3. 如果文中沒特別打錯,invalid syntax應該只是因為if要小寫。 可以這樣改 while res != 'c': if res == 'h': .... 4. 你res拿輸入寫在while迴圈外面,沒其它處理的話,可能會無限迴圈。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.24.176.192 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1498639595.A.B19.html ※ 編輯: crazycy (114.24.176.192), 06/28/2017 16:48:17