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
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: