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><code>except ValueError as err:</code> only happens when there is a ValueError thrown. The value of <code>answer</code> is <code>False</code>, but that is just an arbitrary boolean value, not an error.</p>

<p>See <a href="https://docs.python.org/2/library/exceptions.html#exceptions.ValueError" rel="nofollow"><code>ValueError</code> documentation</a> for examples of things that are errors.</p>

<p>In your case, simply test:</p>

<pre><code>answer = input("\t &gt;&gt; ")
if answer.isalpha():
    print(v0 * t - 0.5 * g * t ** 2)
    break
</code></pre>

python python-3.x