I'm trying to make a code that allows only letters. I know we can do this using isalpha()
method. But, I'm looking for any other different solutions something like try-except?
That is reinventing the wheel, use str.isalpha
You could use assert
and AssertionError
from string import ascii_letters
value = None
while True:
try:
value = input("Give a value: ")
assert all(c in ascii_letters for c in value)
break
except AssertionError:
print("Invalid input, try again")
print("Valid input:", value)
Give a value: aa!
Invalid input, try again
Give a value: !!!
Invalid input, try again
Give a value: !
Invalid input, try again
Give a value: rrTT
Valid input: rrTT