Module Inside If Else Statement in Python?
<p>I'm about four weeks into Python and I love it. I've just finished up a Lab assignment with my lab partner and then I got a random inspiration to create a little Python program for a game my boyfriend is massively obsessed with: Warhammer.</p>

<p>I created a basic one that worked pretty well, but for the advanced program I'm having a little bit of trouble. </p>

<p>What the advanced program does: </p>

<p>You input how many points your game will be</p>

<p>Displays a list of all the Grey Knight HQs, Troops, etc. section by section (first all the HQs, then the Elites, etc.).</p>

<p>Section by section you choose which unit you want by entering the number beside the name</p>

<p>The program gives you a sub total of each section as you go.</p>

<p>The program gives you a total of all of the sections combined at the end.</p>

<p>The program tells you how many points you have remaining to use.</p>

<p>For one of the HQ's for the Grey Knights you can choose to have up to 5 models at 40 points each. In this program I want to list the models out (using the print command) then have if elif statements to correspond to each of the numbers. Here's the section of my code I'm having trouble with:</p>

<pre><code>def inputHQ():

    print

    print

    print 'Select your HQ by entering the number beside their name. Example "1", "2", "3".'

    print 

    print '1. Lord Kaldor Draiog - 275 points'

    print '2. Grand Master Mordrak - 200 points'

    print '3. Ghost Knights - 40 points per model'

        greyKnightHQ = input('What HQ do you want? Use the number beside the modle, no periods: ')

            if greyKnightHQ == 1:

                greyKnightHQ = 275

            elif greyKnightHQ == 2:

                greyKnightHQ = 200

            elif greyKnightHQ == 3:

                greyKnightGhostKnight = input('How many Ghost Knights would you like? Up to 5: ')

                def calcGreyKnightGhostKnight():

                    greyKnightGhostKnightTotal = greyKnightGhostKnight * 40 

                greyKnightHQ = greyKnightGhostKnightTotal

                return greyKnightHQ
</code></pre>

<p>When I run it in Geany I get this error:</p>

<pre><code>Traceback (most recent call last):

        File "warhammer-point-calculator-advanced.py", line 96, in &lt;module&gt;

            main()

        File "warhammer-point-calculator-advanced.py", line 13, in main

            greyKnightHQ = inputHQ()

        File "warhammer-point-calculator-advanced.py", line 48, in inputHQ

           greyKnightHQ = greyKnightGhostKnightTotal

NameError: global name 'greyKnightGhostKnightTotal' is not defined
</code></pre>

<p>Here are my definitions at the top of the document for those functions:</p>

<p><code>
greyKnightHQ = inputHQ()</p>

<p>greyKnightGhostKnightTotal = calcGreyKnightGhostKnight(greyKnightHQ)
</code></p>

<p>My main two questions are:
1. Can you define a module within an if else statement in Python? If so, am I doing it right?
2. Could I also create the calcGreyKnightGhostKnightTotal module outside of the if else statement and just have it called when needed other wise have it multiply by 0 so it doesn't mess up the rest of the program?</p>
