Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
class character():
    def __init__(self):
        self.name = " "
        self.race = " "
        self.characterClass = " "
        self.gender = " "
        self.strength = random.randint(3,18)
        self.dexterity = random.randint(3,18)
        self.constitution = random.randint(3,18)
        self.intelligence = random.randint(3,18)
        self.wisdom = random.randint(3,18)
        self.charisma = random.randint(3,18)
        self.advantage = advantage = False
        self.languages = []
        magic_sleep_immunity = 0 
        low_light_vision = 0
        proficincy = []
        Darkvision = 0
        stonecunning = 0
        stability = 0
            
    def __getRace__():
        races = ['human', 'elf', 'dwarf',]
        racec = input()
        if racec in races:
            if racec == "human":
                race = "human"
                size = "medium"
                languages.append("Common")
                speed = 30
                human_desc = "Humans are versitile in any role but do not have a specialized role"
                print(human_desc)
            elif racec == "elf":
                race = "elf"
                dexterity += 2
                constitution -= 2
                size = "medium"
                speed = 30
                magic_sleep_immunity = 1
                low_light_vision = 1
                proficincy.append("longsword", "rapier", "longbow", "shortbow")
                #Gains advantage on Listen, Search, and Spot checks
                languages.append("Common", "Elvish")
                elf_desc = "Elves are skillful but frail. They are good as wizards and marksman"
                print(elf_desc)
            elif racec == "dwarf":
                race = "race"
                constitution += 2
                charisma -= 2
                size = "medium"
                speed = 20 # However, dwarves can move at this speed even when wearing medium or heavy armor or whose speed is reduced in such conditions).
                Darkvision = 1
                stonecunning = 1
                stability = 1
                proficincy.append("dwarven waraxe", "dwarven urgroshes")
                #Advantage on: Poison, spells, against orcs and gobilnoids, dodging giants, and apraise and craft checks that include metal or precious gems
                languages.append("Common", "Dwarvish")
                dwarf_desc = "Dwarves are tougher but gruff and a bit slower, They are great warriors"


That is my character class. The problem lies within the race portion.
Whenever I run my code and try to pick "dwarf". This happens
Python
Traceback (most recent call last):
  File "main.py", line 36, in <module>
    ac.character.__getRace__()
  File "/usercode/Actual_Functions.py", line 78, in __getRace__
    constitution += 2
UnboundLocalError: local variable 'constitution' referenced before assignment

Any help is appreciated.

What I have tried:

Google and documentation as usual
Posted
Updated 28-Oct-22 22:05pm

All of those variables are defined in the __init__ method of the class. So you need to change the __getRace__ method to:
Python
def __getRace__(self):

and use the self. accessor to refer to the variables.

See 9. Classes — Python 3.10.8 documentation[^] for a full discussion on the scope of variables.
 
Share this answer
 
Read the error message:
Error
local variable 'constitution' referenced before assignment

That means that it cannot find a pre-existing variable called constitution that has been assigned a value before you try to access it's content.
So look at
Error
File "/usercode/Actual_Functions.py", line 78, in __getRace__
and see where the variable is declared and assigned - and why it isn't in scope for the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900