Data Validation Using Functions
<p>How do I create a function that validates the following?</p>

<pre><code>def get_order():
    order=int(input("Select an item number: "))
    return order
</code></pre>

<p>I tried this:</p>

<pre><code>def validate_order(order_choice):
    order_choice=get_order()
    return order_choice
    while order_choice &lt; 1 or order_choice &gt; 8:
        order_choice=int(input("Invalid value. Re-enter item number: "))
        return order_choice
</code></pre>

<p>I am confused on how to use parameters correctly. </p>

<p>Thanks!</p>

<p>I'm going to assume you want to validate that the order number is indeed an integer.</p>

<p>To do this we will use the <code>isinstance()</code> function. Which accepts a variable and a type and returns <code>True</code> or <code>False</code> depending on whether or not it matches the second argument.</p>

<p>In your case it will look like this:</p>

<pre><code>order = input("Select an item number: ")
while isinstance(order, int) or order not in xrange(1,9):
    order = get_order("Invalid value. Re-enter item number: ")
</code></pre>

python function validation