Keyboard Input Check

Hi.

First off, you don’t need x = str(input("enter a string? ")) because the input() function will return a string by default, so: x = input("enter a string? ")

Maybe something like this, if you want to find out if any of the characters is not ‘alpha’ or a space?

x = input("enter a string? ")

for char in x:
    if char.isalpha() or char == ' ':
        pass
    else:
        print("Error in input")
        break

You need to better define what your input will and will not accept as ‘valid’ or ‘invalid’.

Note: any character that is not ‘a’ to ‘z’ or ‘A’ to ‘Z’ will fail the above test.

edit to add:

Maybe a better way would be to use this, to filter just for numbers:

for char in x:
    if char.isdigit():
        print("Error in input")
        break
2 Likes