我的 python 程序有两次调用raw_input()
首先raw_input()
是从用户那里获取多行输入。用户可以发出 Ctrl+D(在 Windows 中为 Ctrl+Z)来结束输入。
Second raw_input()
应该使用 (y/n) 类型提示从用户处获取另一个输入。
不幸的是(仅在 Mac OS X 中?),第二raw_input()
raises EOFError
当标准输入首先终止时(使用 Ctrl+D)raw_input()
prompt.
请参阅下面的示例代码以获取更多说明 -
mailBody = ''
signature = 'Later!'
print 'Compose your mail:'
while True:
try:
# Hit ^D after entering some text
mailBody+= raw_input()
mailBody+='\n'
except EOFError:
break
# This raw_input() throws EOFError too. Because, stdin is terminated for the session
# when EOF (^D) is issues at first raw_input() method (Where as, it doesn't raise EOFError in Linux)
opt = raw_input("Do you want to add signature to your mail? (y/N): ").lower()
print '-'*10+'Your Mail'
if opt == 'y':
print mailBody+"\n"+signature
else:
print mailBody
print '-'*19
程序输出:
-1- abhinay@MacBook code/py % python prompt.py
Compose your mail:
hello there!
how is everybody?
Do you want to add signature to your mail? (y/N): Traceback (most recent call last):
File "prompt.py", line 11, in <module>
opt = raw_input("Do you want to add signature to your mail? (y/N): ").lower()
EOFError
我怎样才能让第二次提示不加注EOFError
。请帮忙!
EDIT:
我编辑了我的问题以使其简单。
我在Linux系统中运行了上面的代码,它运行没有任何问题。也就是说,在第二个 raw_input() 处提示用户接收“(y/N)”选择。