Python - No valuerror on int with isalpha
<p>Why is no ValueError raised on this try / except when isalpha should fail.</p>

<p>I know that isalpha returns false if given a number</p>

<pre><code>In [9]: ans = input("Enter a Letter")
Enter a Letter4

In [10]: ans.isalpha()
Out[10]: False
</code></pre>

<p>How do I get the value error if they supply a number instead of a y or n? Because if the try is false shouldn't it stop being true and not print my trajectory?</p>

<pre><code>import sys

v0 = float(input("What velocity would you like? "))
g = float(input("What gravity would you like? "))
t = float(input("What time decimal would you like? "))

print("""
    We have the following inputs.
       v0 is %d
       g is  %d
       t is  %d
    Is this correct? [Y/n]
""" % (v0, g, t))
while True:
    try:
        answer = input("\t &gt;&gt; ").isalpha()
        print(v0 * t - 0.5 * g * t ** 2)
    except ValueError as err:
        print("Not a valid entry", err.answer)
        sys.exit()
    finally:
            print("would you like another?")
            break
</code></pre>

<p>For example if the user types 5 not a y or n still gets an answer </p>

<pre><code>$ python3 ball.py 
What velocity would you like? 2
What gravity would you like? 3
What time decimal would you like? 4

    We have the following inputs.
       v0 is 2
       g is  3
       t is  4
    Is this correct? [Y/n]

     &gt;&gt; 5
-16.0
would you like another?
</code></pre>

<p>Posting an answer for clarity trying to provide a way to be more consistent and explicit with the treatment of <em>strings</em> and <em>int's</em>. By using <em>isinstance</em> I declare to a person reading my code explicitly what my values are to be hopefully improving readability.</p>

<pre><code>answer = input("\t &gt;&gt; ")
if isinstance(int(answer), int) is True:
    raise ValueError("Ints aren't valid input")
    sys.exit()
elif isinstance(answer, str) is True:
        print(v0 * t - 0.5 * g * t ** 2)
else:
    print("Ok please ammend your entries")
</code></pre>

<p>Should I have differing requirements later this could easily be abstracted into a function, which because isinstance allows checking against multiple types increases flexibility.</p>

<p><strong>Reference</strong> <a href="http://stackoverflow.com/a/11204870/461887">How to properly use python's isinstance() to check if a variable is a number?</a></p>

<pre><code>def testSomething(arg1, **types):
    if isintance(arg1, [types]):
        do_something
</code></pre>

python python-3.x