How can I raise an error if input is NaN?
<p>I am trying to implement an algorithm that converts a decimal number to is binary equivalent.</p>

<p>This is what I have. </p>

<pre><code>def binary_converter(n):
    if n &lt; 0:raise ValueError, "Invalid input"
    if n &gt;255:raise ValueError, "Invalid input"
    if n &gt; 1:
        binary_converter(n//2)
        print(n % 2,end = '')

# Take decimal number from user
dec = int(input("Enter an integer: "))
binary_converter(dec)
</code></pre>

<p>My question is about these lines:</p>

<pre><code>    if n &lt; 0:raise ValueError, "Invalid input"
    if n &gt;255:raise ValueError, "Invalid input"
</code></pre>

<p>I am attempting to validate the input contains only digits 0-9.</p>

<p>How can I achieve that validation?</p>

<p>A couple of methods. One of which is using the strings <code>.isdigit()</code>. The general problem with <code>.isdigit()</code> is that it doesn't work on negatives, however with your code, it really isn't a problem. Try replacing your input with the following custom function:</p>

<pre><code>def positive_int_input():
    s = input("Enter an integer: ")
    if not s.isdigit():
        raise ValueError("Input is not a positive integer")
    else:
        return int(s)
</code></pre>

<p>A few examples:</p>

<pre><code>positive_int_input()
#   Enter an integer: 42
#-&gt; 42

positive_int_input()
#   Enter an integer: 3.14
#   ValueError: Input is not an integer

positive_int_input()
#   Enter an integer: Hello World!
#   ValueError: Input is not an integer
</code></pre>

<p>That being said:</p>

<pre><code>int("Some String")
# ValueError: invalid literal for int() with base 10: 'Some String'
</code></pre>

<p>An Error is returned regardless, so I don't know exactly what you think is wrong with your code besides fixing up the indenting.</p>

python