如何知道用户是否使用Python按 Enter ?
例如:
user = raw_input("type in enter")
if user == "enter":
print "you pressed enter"
else:
print "you haven't pressed enter"
答案 0 :(得分:9)
正如@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
函数的唯一其他方法会引发异常,例如:
EOFError
,则^D
如果您输入KeyboardInterrupt
^C
答案 1 :(得分:-3)
user_input=input("ENTER SOME POSITIVE INTEGER : ")
if((not user_input) or (int(user_input)<=0)):
print("ENTER SOME POSITIVE INTEGER GREATER THAN ZERO") #print some info
import sys #import
sys.exit(0) #exit program
'''
#(not user_input) checks if user has pressed enter key without entering
# number.
#(int(user_input)<=0) checks if user has entered any number less than or
#equal to zero.
'''