Следующий блок кода:
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'.
Но цикл никогда не останавливается. Должно быть что-то, чего мне не хватает.
Пожалуйста, помогите мне.
EDIT:
Это тот же результат того же кода при запуске в интерактивном режиме:
Версия 3.2.0 на компьютере с Windows 7.
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