Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:52:09+00:00 2026-05-26T14:52:09+00:00

The following block of code: ans = ‘x’ while ans not in [‘Y’,’y’,’N’,’n’]: ans

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:52:09+00:00Added an answer on May 26, 2026 at 2:52 pm

    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:

    In Python 3.2, the builtin function input() returns a string with a
    trailing ‘\r’ on windows:

    C:\Python32>python
    Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print(repr(input()))
    test
    'test\r'
    >>>
    

    Time line:

    • 2011-02-20 3.2.0 released
    • 2011-02-21 issue reported
    • 2011-02-23 issue fixed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really stuck on why the following code block 1 result in output 1
I have the following block code typedef BOOL(^FieldValidationBlock)(NSString *); FieldValidationBlock aBlock = ^(NSString *input){
Consider the following block of code- if (1==1):#Go forever print Wooo. Vim doesn't see
I have added the following block of code to my WP7 app and not
I'm reviewing java and stumbled upon something like the following block of code public
When running the following block of code, FF and Chrome output typeof(hiya) = string
In the following block of code, I am trying to print the properties whose
I am getting the following output from the following block of code: //create file
Consider the following block of code: class CheckStore { private String displayText; private boolean
i have the following block of code: $(#contact_container form, #contact_details form).live( submit, function(event) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.