0

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.

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

What the advanced program does:

You input how many points your game will be

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

Section by section you choose which unit you want by entering the number beside the name

The program gives you a sub total of each section as you go.

The program gives you a total of all of the sections combined at the end.

The program tells you how many points you have remaining to use.

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:

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

When I run it in Geany I get this error:

Traceback (most recent call last):

        File "warhammer-point-calculator-advanced.py", line 96, in <module>

            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

Here are my definitions at the top of the document for those functions:

greyKnightHQ = inputHQ()

greyKnightGhostKnightTotal = calcGreyKnightGhostKnight(greyKnightHQ)

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?

1
  • 1
    I don't see you creating or importing a module inside your function, but a function. You can have functions defined in functions, but I don't think you need to do that here. Especially since you never call it. A function creates a new scope (namespace), and the gKGKT variable only exists inside it. Therefore it does not exist in the outer function. But really, this is a good opportunity to learn about classes. Commented Sep 19, 2012 at 4:01

1 Answer 1

2

You're getting problems because you're creating a function within a function and then trying to access it out of its scope:

def inputHQ():
    ...

    def calcGreyKnightGhostKnight():
        ...

inputHQ is defined globally, but calcGreyKnightGhostKnight is only defined within inputHQ. Trying to access calcGreyKnightGhostKnight outside of inputHQ will yield a NameError.

I'm not exactly sure what inputHQ is really doing, so I can't offer you much more help than this.

Sign up to request clarification or add additional context in comments.

4 Comments

inputHQ is supposed to contain all of the elements for selecting which HQ you want to use. Once the HQ is selected, it will be added tot the total at the end of the program as well as a subtotal at the end of the HQ Selection section. Let me know if I'm using modules incorrectly. Like I said, I'm only about four weeks into Python.
No, not yet. Though I'm very familiar with CSS classes if they're anything a like.
They're only similar in name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.