楼主:
crazycy (LCY)
2017-06-28 16:46:33※ 引述《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循环外面,没其它处理的话,可能会无限循环。