Try and Except (TypeError)
<p>What I'm trying to do is create a menu-style start in my program that let's the user choose whether they want to validate a code or generate one. The code looks like this:</p>

<pre><code>choice = input("enter v for validate, or enter g for generate").lower()

try:
   choice == "v" and "g"

except TypeError:
   print("Not a valid choice! Try again")
   restartCode()    *#pre-defined function, d/w about this*
</code></pre>

<p>So I would like my program to output that print statement and do that defined function when the user inputs something other than "v" or "g" (not including when they enter capitalised versions of those characters). 
There is something wrong with my try and except function, but whenever the user inputs something other than those 2 characters the code just ends.</p>

<p>You are confused about what <code>try/except</code> does. <code>try/except</code> is used when an error is likely to be raised. No error will be raised because everything in your program is valid. Errors are raised only when there is an execution error in your code. Errors are not just raised when you need them to be.</p>

<p><em>You</em>, however, want an error to be shown if the user does not enter a valid choice. You need to use an <code>if/else</code> logic instead, and print the error out yourself. And as a side note, the line <code>choice == "v" and "g"</code> does not test if choice is equal to <code>'v'</code> or <code>'g'</code>. It test if choice i equal to v and if the string <code>'g'</code> is "truthy". Your estenially saying</p>

<p><code>if variable = value and True</code></p>

<p>I'm pretty sure that is not what you want. Here is how I would re-write your code.</p>

<pre><code>if choice.lower() in {"v", "g"}: # if choice is 'v' or 'g'
    # do stuff
else: # otherwise
    print("Not a valid choice! Try again") # print a custom error message.
</code></pre>

python try-catch typeerror except