在 while 循环中从键盘读取字符
以下代码块:
ans = 'x'
while ans not in ['Y','y','N','n']:
ans = input("Do Something? [y|n]:")
print(ans in ['Y','y','N','n'])
产生以下输出:
Do Something? [y|n]:Y
False
Do Something? [y|n]:y
False
Do Something? [y|n]:N
False
Do Something? [y|n]:n
False
Do Something? [y|n]:asdf
False
Do Something? [y|n]:Traceback (most recent call last):
File "./NumberPatterns.py", line 27, in <module>
ans = input("Do Something? [y|n]:")
KeyboardInterrupt
我想重复读取用户的输入,直到它是“Y”,“y”,“N”,“n”。 但循环永远不会停止。一定有什么东西是我遗漏的。 请帮我。
编辑:
在交互模式下运行时,这是相同代码的相同结果: Windows 7 计算机上的版本为 3.2.0。
C:\Users\jwalker>python
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ans = 'x'
>>> while ans not in ['Y','y','N','n']:
... ans = input("Do Something? :")
... print(ans in ['Y','y','N','n'])
... print(ans, type(ans), len(ans), ord(ans[0]), repr(ans))
... print('Y', type('Y'), len('Y'), ord('Y'), repr('Y'))
...
Do Something? :asdf
False
<class 'str'> 5 97 'asdf\r'
Y <class 'str'> 1 89 'Y'
Do Something? :Y
False
<class 'str'> 2 89 'Y\r'
Y <class 'str'> 1 89 'Y'
Do Something? :n
False
<class 'str'> 2 110 'n\r'
Y <class 'str'> 1 89 'Y'
Do Something? :Traceback (most recent call last):
File "<stdin>", line 2, in <module>
>>>
>>> ^Z
The following block of code:
ans = 'x'
while ans not in ['Y','y','N','n']:
ans = input("Do Something? [y|n]:")
print(ans in ['Y','y','N','n'])
produces the following output:
Do Something? [y|n]:Y
False
Do Something? [y|n]:y
False
Do Something? [y|n]:N
False
Do Something? [y|n]:n
False
Do Something? [y|n]:asdf
False
Do Something? [y|n]:Traceback (most recent call last):
File "./NumberPatterns.py", line 27, in <module>
ans = input("Do Something? [y|n]:")
KeyboardInterrupt
I want to read the user's input repeatedly, until it is 'Y','y','N','n'.
But the loop never stops. There must be something that i am missing.
Please help me.
EDIT:
This is the same result of the same code, when run in Interactive Mode:
Version is 3.2.0 on a Windows 7 machine.
C:\Users\jwalker>python
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ans = 'x'
>>> while ans not in ['Y','y','N','n']:
... ans = input("Do Something? :")
... print(ans in ['Y','y','N','n'])
... print(ans, type(ans), len(ans), ord(ans[0]), repr(ans))
... print('Y', type('Y'), len('Y'), ord('Y'), repr('Y'))
...
Do Something? :asdf
False
<class 'str'> 5 97 'asdf\r'
Y <class 'str'> 1 89 'Y'
Do Something? :Y
False
<class 'str'> 2 89 'Y\r'
Y <class 'str'> 1 89 'Y'
Do Something? :n
False
<class 'str'> 2 110 'n\r'
Y <class 'str'> 1 89 'Y'
Do Something? :Traceback (most recent call last):
File "<stdin>", line 2, in <module>
>>>
>>> ^Z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的 Python 更新到更新版本。
您遇到了 3.2.0 中引入的错误,并且几乎立即修复了。从错误报告来看:
时间线:
Update your Python to a more recent version.
You have run afoul of a bug that was introduced in 3.2.0 and almost immediately fixed. From the bug report:
Time line:
该代码在 Python 3.2 中运行得很好:
这让我怀疑你的问题比你的基本示例更复杂。你在哪里运行代码?它是在循环内部的函数中吗?
That code works perfectly well in Python 3.2:
This makes me suspect that your question is a hint more complicated than your basic example. Where are you running the code? Is it in a function which is inside of a loop?
编辑此答案适用于 Python-2,因此这不是回答标有 Python-3.x 的问题。请不要考虑它。
根据 Python 文档,您应该使用
raw_input()
:那是因为
input()
不是您所期望的:正如您的评论所强调的那样,这并不是您真正需要的。
EDIT This answer is for Python-2, so this is not answering to a question tagged with Python-3.x. Please do not consider it.
According to Python documentation, you should use
raw_input()
:That's because
input()
is not what you expect:This is not really what you need here, as highlighted by your comment.
尝试使用 strip() 修剪文本:
try trimming the text with strip():