Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:32:47+00:00 2026-06-11T13:32:47+00:00

I’m about four weeks into Python and I love it. I’ve just finished up

  • 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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:32:48+00:00Added an answer on June 11, 2026 at 1:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.