0

I have a problem here.

class Player:

    def __init__(self,name):
        self.armed = 0
        self.arms = 1
        self.bombs = 0
        self.exposed = 0
        self.foodDrink = 20
        self.gold = 350
        self.happy = 50
        self.heat = 0
        self.heatTier = 0
        self.month = 1
        self.name = name
        self.nameWait = 0
        self.path = 'None'
        self.poster = 5
        self.propa = 0
        self.raidPts = 0
        self.status = 'Citizen'
        self.successRiot = 0
        self.support = 0
        self.raidPts = 0
        self.triedVote = 0
# stuff, mainly for fixing variables down from here...

Another, related block of code.

def nameType():
print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\n')
CharName = input()
if CharName != '':
    if len(CharName) > 20:
        print('► The name is too long!')
        nameType()
    else:
        print('► Are you sure this is what you want as a name?\nIf so, type "yes".\nOtherwise, type "no".')
        confirmName = input()

        if confirmName == 'yes':
            PlayerIG = Player(CharName)
            print('► Crew Member: Thanks for travelling with us, and have a great time,',PlayerIG.name,'.')
            MainMenu()

        if confirmName == 'no':
            nameType()
        else:
            print('► Invalid Input!')
            nameType()
else:
    print('► Empty! Please type again.')
    nameType()

And one last related one (there may be more).

def MainMenu():    
    blankSmall()
    print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\nMonth',PlayerIG.month,'\n▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬')
    blankSmall()
    print('► You can buy items at the store by typing "store",\nadd propaganda by typing "propa",\nmanage the party by typing "manage", or if you have at least 1 bomb and 5 armed supporters,\nyou can conduct a raid by typing "raid".\nWhen you are done for the month, type "end".\nIf you are at rank President, you can participate in an election with "vote".\nIf you are Chief of Revolution, you can conduct a riot with "riot".\nTo save the game and leave, type "save".')
Option = input()
# Again, more stuff down here that may be included...

The problem is, when I load the main menu with MainMenu(), it raises this error:

Traceback (most recent call last):
  File "C:\Users\ONIE\AppData\Local\Programs\Python\Python36-
32\TheGameOfHierarchyAndAnarchy.py", line 791, in <module>
    TitleScreen()
  File "C:\Users\ONIE\AppData\Local\Programs\Python\Python36-
32\TheGameOfHierarchyAndAnarchy.py", line 144, in TitleScreen
    nameType()
  File "C:\Users\ONIE\AppData\Local\Programs\Python\Python36-
32\TheGameOfHierarchyAndAnarchy.py", line 168, in nameType
    MainMenu()
  File "C:\Users\ONIE\AppData\Local\Programs\Python\Python36-
32\TheGameOfHierarchyAndAnarchy.py", line 181, in MainMenu

 print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\nMonth',PlayerIG.month,'\n▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬')
NameError: name 'PlayerIG' is not defined

Is there any solution to this?

EDIT: The MainMenu() function was supposed to be indented, but the formatting was a bit stubborn.

S.G. Harmonia
  • 297
  • 2
  • 18
  • Lets start with your code blocks arent formatted correctly. You have `def` but then all subsequent is left justified and should be indented. Fix the post with correct formatting (as it would look in your code). – pstatix Jun 04 '17 at 01:27
  • What does 'you have `def` but then all subsequent is left justified and should be indented' mean? Also, which function do I change and how? – S.G. Harmonia Jun 04 '17 at 01:32
  • You have functions defined, hence the `def` in your code. But then your code is all to the left hand side. Function code should be indented under the respective `def` for which it is being written. – pstatix Jun 04 '17 at 01:35

2 Answers2

0

Your issue is in the main menu method. You have never defined the PlayerIG variable inside the function. See http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html

Btw, please respect the naming convention for variables. It should be player_ig

Use global variable is bad idea most of the time. The nameType method should return player_ig, then once you call your method you affect the nameType method to a new variable like bellow.

def MainMenu():    
    blankSmall()
    player_ig_in_main_menu_scope = nameType() # you can call it player_ig if you want 
print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\nMonth', player_ig_in_main_menu_scope.month,'\n▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬')
    blankSmall()
M07
  • 1,060
  • 1
  • 14
  • 23
0

The line PlayerIG = Player(CharName) in the nameType() function defines a local variable. You can't reference this outside that function. This is like the following example:

>>> def foo():
...   x = 1
... 
>>> foo()
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

If you want it to be a global variable, you should declare it, like so:

>>> def foo():
...   global x
...   x = 1
... 
>>> foo()
>>> x
1

see also Short Description of the Scoping Rules?

Dave Costa
  • 47,262
  • 8
  • 56
  • 72