python - Prevent input() from being anything but alphabet characters -


i attempting make program sake of self-knowledge. want ask user name is, , want user able use letters alphabet answer, or strings. not want them able answer numbers, symbols, etc.

def cc():   name = (input("""hello, happens first name?           > """))   if type(name) str:       print("you have entered name correctly.")   elif type(name) int:       print("your name cannot integer. try again.")       cc()  cc() 

you can enforce requirement using str.isalpha. documentation:

return true if characters in string alphabetic , there @ least 1 character, false otherwise. alphabetic characters characters defined in unicode character database “letter”, i.e., general category property being 1 of “lm”, “lt”, “lu”, “ll”, or “lo”. note different “alphabetic” property defined in unicode standard.

here example program:

while true:     name = input('enter name using alphabetic characters: ')     if name.isalpha():         break 

demo:

enter name using alphabetic characters:  bo2 enter name using alphabetic characters:  bo^&*( enter name using alphabetic characters:  bob 

note method not work people hyphenated names such "anne-marie".


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -