在控制台中检测回车return的输入
Detecting the input of a carriage return in the console
学校作业的要求我要求当一个回车 return (\r) 作为输入输入时,它用于停止程序:“if only a carriage-return 被输入,停止程序”。我已经尝试了几件事,但对解决方案感到困惑。
以下是我检查过的选项,所有选项都未通过 运行 测试。
while input != '':
while input != '\r':
while input != '\n':
谢谢!
input
是一个函数。你必须写 input()
.
但是,我假设您实际上是在尝试从用户那里读取信息,如果他们什么都没输入就退出。你的声明 while input() != '':
不会那样做,因为如果他们输入有用的东西,它就会被丢弃。你还没有储存它。
您可能想要这样的东西:
while 1:
line = input()
if not line:
break
# Now use the contents of line.
学校作业的要求我要求当一个回车 return (\r) 作为输入输入时,它用于停止程序:“if only a carriage-return 被输入,停止程序”。我已经尝试了几件事,但对解决方案感到困惑。
以下是我检查过的选项,所有选项都未通过 运行 测试。
while input != '':
while input != '\r':
while input != '\n':
谢谢!
input
是一个函数。你必须写 input()
.
但是,我假设您实际上是在尝试从用户那里读取信息,如果他们什么都没输入就退出。你的声明 while input() != '':
不会那样做,因为如果他们输入有用的东西,它就会被丢弃。你还没有储存它。
您可能想要这样的东西:
while 1:
line = input()
if not line:
break
# Now use the contents of line.