Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to avoid integers,floats and special characters using try-except statement only?

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading