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?