Foolproofing a Python calculator
<p>I'm writing a basic calculator which works with two different numbers.
So far, I managed to write a working prototype, but while dividing and foolproofing it I ran into a multitude of problems, so I'm posting them 
separately. </p>

<hr>

<p>I want the program to repeat the question if the user doesn't provide an eligible operator. That's the code I have now: </p>

<pre><code>def optn_query():
    print("Hulk can different things with number!")
    print("YOU!")
    optn = input("What Hulk do with number?! ")
    return optn
</code></pre>

<p><strong>Do I use an if statement to determine if the input is correct?</strong></p>

<hr>

<p>Also I put return optn in there so the next function (gracefully called hulk_math) wouldn't fail midway, but it still does: </p>

<pre><code>Traceback (most recent call last):
  File "hulc.py", line 57, in &lt;module&gt;
    main()
  File "hulc.py", line 13, in main
    hulk_math()
  File "hulc.py", line 41, in hulk_math
    if optn == "+":
NameError: name 'optn' is not defined
</code></pre>

<p>What should I do to fix this?</p>

<p>Here's hulk_math() itself: </p>

<pre><code>def hulk_math():
    if optn == "+":
        result = num1 + num2
        print("Hulk ADDS!!! Hulk thinks it's {0}!".format(result))
    elif optn == "-":
        result = num1 - num2
        print("Hulk SUBTRACTS!!! Hulk thinks it's {0}!".format(result))
    elif optn == "*":
        result = num1 * num2
        print("Hulk MULTIPLIES!!! Hulk thinks it's {0}!".format(result))
    elif optn == "/":
        result = num1 / num2
        print("Hulk DIVIDES!!! Hulk thinks it's {0}!".format(result))
    main()
</code></pre>

<p>Using <code>global</code> isn't the right way to do this.  Pass values from one function to another by saving their return values and passing them as arguments.</p>

<pre><code>def main():
    intro()
    num1 = num1_query()
    optn = optn_query()
    num2 = num2_query()
    hulk_math(num1, optn, num2)

def hulk_math(num1, optn, num2):
    #Your original code will work as expected
</code></pre>

python python-3.x object calculator