# Keyboard Input Check

**URL:** <https://discuss.python.org/t/keyboard-input-check/18818>\
**Category:** Python Help\
**Tags:** help\
**Created:** [September 6, 2022, 1:40pm UTC](https://discuss.python.org/t/keyboard-input-check/18818 "2022-09-06T13:40:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![osd.data](https://avatars.discourse-cdn.com/v4/letter/o/8491ac/32.png) [@osd.data](https://discuss.python.org/u/osd.data)\
**Post date:** [September 6, 2022, 1:40pm UTC](https://discuss.python.org/t/keyboard-input-check/18818/1 "2022-09-06T13:40:27Z")

</div>

I try to check input using:  
try:  
x = str(input("enter a string? "))  
except:  
print(“Invalid.”)

I enter a number at the prompt and it works. How do I detect the wrong input?

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [September 6, 2022, 2:11pm UTC](https://discuss.python.org/t/keyboard-input-check/18818/2 "2022-09-06T14:11:36Z")

</div>

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?

```python
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:

```python
for char in x:
    if char.isdigit():
        print("Error in input")
        break

```

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [September 7, 2022, 12:42am UTC](https://discuss.python.org/t/keyboard-input-check/18818/3 "2022-09-07T00:42:24Z")

</div>

Why is a string made up of digits “wrong input”? It is still a string of characters.

There is no point in calling `str()` on the result of `input()`, because `input()` already returns a string. So that’s just a waste of time which does nothing.

Nothing inside the `try` block can fail, so the `except` clause will never run. Which is good, because …

This sort of `try...except` block is one of [the most diabolically awful things you can write](https://realpython.com/the-most-diabolical-python-antipattern/), trust me, do not get into the habit of doing this!

```python
# This is bad, don't do it!
try:
    code
except:
    print("Invalid")

```

The Python interpreter makes a **huge** effort to give the programmer a lot of information about the error, and why it happened, and you replace that with just “Invalid”. That’s terrible! As a programmer, there are very few things worse than such _pointless_ and uninformative error messages as “Invalid” and “An error occurred”.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [September 7, 2022, 2:00am UTC](https://discuss.python.org/t/keyboard-input-check/18818/4 "2022-09-07T02:00:47Z")

</div>

If you are only looking to reject numeric characters (digits) you can write this:

```python
if any(c.isdigit() for c in x):
    print("Found a digit")

```
