raw_input should accept only single character

python

Solution

The following will continuously prompt the user for input until they enter exactly one character.

userInput = ''
while len(userInput) != 1:
    userInput = raw_input(':')
guessInLower = userInput.lower()

This does the same, but also informs them of the one character limit before prompting again for input

while True:
    userInput = raw_input(':')
    if len(userInput) == 1:
        break
    print 'Please enter only one character'
guessInLower = userInput.lower()

It looks like you are expecting only letters. If that is the case you can expand this further to require that:

import string

while True:
    userInput = raw_input(':')
    if len(userInput) == 1:
        if userInput in string.letters:
            break
        print 'Please enter only letters'
    else:
        print 'Please enter only one character'
guessInLower = userInput.lower()

Problem

Possible Duplicate: Python read a single character from the user I am using below code.But instead of accepting a single character its allowing user to put more than a single character. How can I fix that? ``` guess = raw_input(':') guessInLower = guess.lower() ```

Original source

Related problems