Restricting the User Input to Alphabets
Question: I'm a technical writer learning python. I wanted to write a program for
validating the Name field input,as a practise, restricting the the user
entries to alphabets.I saw a similar code for validating number (Age)field
here, and adopted it for alphabets as below:

    
    
    import string
    

`import re`

    
    
    r = re.compile(r'[a-zA-Z]+') 
    print "WELCOME FOR NAME VERIFICATION. TYPE ALPHABETS ONLY!"
    print raw_input("Your Name:")
    x = r
    if x == r:
        print x
    elif x != r:
        print "Come on,'", x,"' can't be your name"
        print raw_input("Your Name:")
    if 5<=len(x)<=10:
        print "Hi,", x, "!"
    elif len(x)>10:
        print "Mmm,Your name is too long!"
    elif len(x)<5:
        print "Alas, your name is too short!"
    
    raw_input("Press 'Enter' to exit!")
    

I intend this code block to do two things. Namely, display the input prompt
until the user inputs alphabets only as 'Name'. Then, if that happens, process
the length of that input and display messages as coded. But, I get two
problems that I could not solve even after a lot of attempts. Either, even the
correct entries are rejected by exception code or wrong entries are also
accepted and their length is processed.

Please help me to debug my code. And, is it possible to do it without using
the reg exp?


Answer: If you're using Python, you don't need regular expressions for this--there are
included libraries which include functions which might help you. From this
page on [String methods](http://docs.python.org/release/2.5.2/lib/string-
methods.html), you can call `isalpha()`:

> Return true if all characters in the string are alphabetic and there is at
> least one character, false otherwise.

I would suggest using `isalpha()` in your if-statement instead of `x==r`.

