+-

如何知道用户是否使用 Python按Enter键?
例如 :
user = raw_input("type in enter")
if user == "enter":
print "you pressed enter"
else:
print "you haven't pressed enter"
最佳答案
正如@jonrsharpe所说,正确退出raw_input函数的唯一方法是按Enter键.因此,解决方案是检查结果是否包含某些内容:
text = raw_input("type in enter")
if text == "":
print "you pressed enter"
else:
print "you typed some text before pressing enter"
我看到退出raw_input函数的唯一其他方法会引发异常,例如:
>如果输入^ D,则为EOFError
>如果键入^ C,则为KeyboardInterrupt
> ……
点击查看更多相关文章
转载注明原文:如何知道用户是否使用Python按Enter键 - 乐贴网