lundi 15 février 2021

Function vs if-statement: Function is not working, but the code in the function will work when outside a function?

I was working on building a randomized character generator for Pathfinder 3.5 and got stuck.

I am using the Populate_Skills(Skill_String, Draw, Skill_List, Class_Skill): function to populate a randiomized list of skills with their class based points total, class bonus, and point buy. So modelling the action of a player picking skills for their character.

As an example below, Wizards.

I pick Knowledge_Arcana as a skill and spend one of my skill point pool (Calculated by taking my intelligence modifier +2) on it. So that skill now equals my intelligence modifier(+1 in this case), class skill bonus as a wizard (+3), plus the point I spent(+1) for a total of 5.

The problem is while the function prints the correct result of 5, the outstanding variables do not populate with the final total. To continue our example I'd run the function on Knowledge_Arcana, get a +5, and then check the Knowledge_Arcana after the function call and get just +1. Conversely, if I write out the function as just an if statement it works. Example is next to the function for comparison.

Does anyone know why Im getting the different result?


import random
import pandas as pd
import numpy as np

## Creating the lists and breaking into two separate sections
Str_Mod = 1
Dex_Mod = 1
Wis_Mod = 1
Cha_Mod = -1
Con_Mod = -1
Int_Mod = 1
Rand_Class = ['Wizard']

if Rand_Class == 'Barbarian':
    Skill_Ranks = 4 + Int_Mod
elif Rand_Class == 'Bard':
    Skill_Ranks = 4 + Int_Mod
elif Rand_Class == 'Cleric':
    Skill_Ranks = 2 + Int_Mod
elif Rand_Class == "Druid":
    Skill_Ranks = 4 + Int_Mod
elif Rand_Class =="Fighter":
    Skill_Ranks = 2 + Int_Mod
elif Rand_Class =="Monk":
    Skill_Ranks = 4 + Int_Mod
elif Rand_Class =="Paladin":
    Skill_Ranks = 2 + Int_Mod
elif Rand_Class =="Ranger":
    Skill_Ranks = 6 + Int_Mod
elif Rand_Class =="Rogue":
    Skill_Ranks = 8 + Int_Mod
elif Rand_Class =="Sorcerer":
    Skill_Ranks = 2 + Int_Mod
elif Rand_Class =="Wizard":
    Skill_Ranks = 2 + Int_Mod
else:
    print("Useless.")

Climb = Str_Mod
Swim = Str_Mod
Str_Mod_Skills = [Climb, Swim]
Bluff = Cha_Mod
Diplomacy = Cha_Mod
Disguise = Cha_Mod
Handle_Animal = Cha_Mod
Intimidate = Cha_Mod 
Perform = Cha_Mod
Use_Magic_Device = Cha_Mod
Cha_Mod_Skills = [Bluff, Diplomacy, Disguise, Handle_Animal, Intimidate, Perform, Use_Magic_Device]
Acrobatics = Dex_Mod 
Disable_Device = Dex_Mod 
Escape_Artist = Dex_Mod 
Fly = Dex_Mod 
Ride = Dex_Mod 
Sleight_of_Hand = Dex_Mod 
Stealth = Dex_Mod
Dex_Mod_Skills = [Acrobatics, Disable_Device, Escape_Artist, Fly, Ride, Sleight_of_Hand, Stealth]
Appraise = Int_Mod 
Craft = Int_Mod
Knowledge_Arcana  = Int_Mod 
Knowledge_Dungeoneering = Int_Mod 
Knowledge_Engineering = Int_Mod 
Knowledge_Geography = Int_Mod
Knowledge_History = Int_Mod
Knowledge_Local = Int_Mod
Knowledge_Nature = Int_Mod
Knowledge_Nobility = Int_Mod
Knowledge_Planes = Int_Mod 
Knowledge_Religion = Int_Mod 
Linguistics = Int_Mod
Spellcraft = Int_Mod
Int_Mod_Skills = [Appraise, Craft, Knowledge_Arcana, Knowledge_Dungeoneering, Knowledge_Engineering, Knowledge_Geography,
Knowledge_History, Knowledge_Local, Knowledge_Nature, Knowledge_Nobility, Knowledge_Planes, Knowledge_Religion, 
Linguistics, Spellcraft]
Heal = Wis_Mod
Perception = Wis_Mod
Profession  = Wis_Mod
Sense_Motive  = Wis_Mod
Survival = Wis_Mod
Wis_Mod_Skills = [Heal, Perception, Profession, Sense_Motive, Survival]

Barbarian_Class_Top_Skills =["Intimidate", "Perception", "Survival"]
Bard_Class_Top_Skills =["Appraise", "Bluff", "Diplomacy"]
Cleric_Class_Top_Skills = ["Diplomacy", "Heal", "Knowledge_Religion"]
Druid_Class_Top_Skills = ["Handle_Animal", "Knowledge_Nature", "Perception"]
Fighter_Class_Top_Skills =["Climb", "Survival", "Swim"]
Monk_Class_Top_Skills = ["Acrobatics", "Escape_Artist", "Perception"]
Paladin_Class_Top_Skills = ["Diplomacy", "Knowledge_Nobility", "Sense_Motive"]
Ranger_Class_Top_Skills = ["Knowledge_Geography", "Perception", "Survival"]
Rogue_Class_Top_Skills = ["Disable_Device", "Sleight_of_Hand", "Stealth"]
Sorcerer_Class_Top_Skills = ["Knowledge_Arcana", "Spellcraft", "Use_Magic_Device"]
Wizard_Class_Top_Skills = ["Fly", "Knowledge_Arcana", "Spellcraft"]

Barbarian_Class_Less_Skills =["Acrobatics", "Climb", "Craft", "Handle_Animal", "Knowledge_Nature", "Ride", "Swim"]
Bard_Class_Less_Skills =["Acrobatics", "Climb", "Craft", "Disguise", "Escape_Artist", "Intimidate", 
                         "Knowledge_Arcana", "Knowledge_Dungeoneering", "Knowledge_Engineering", 
      "Knowledge_Geography", "Knowledge_History", "Knowledge_Local", "Knowledge_Nature", "Knowledge_Nobility", 
       "Knowledge_Planes", "Knowledge_Religion", "Linguistics", "Perception", "Perform", "Profession", 
                         "Sense_Motive", "Sleight_of_Hand", "Spellcraft", "Stealth", "Use_Magic_Device"]
Cleric_Class_Less_Skills = ["Appraise", "Craft", "Knowledge_Arcana", "Knowledge_History", "Knowledge_Nobility", 
                            "Knowledge_Planes",
                      "Linguistics", "Profession", "Sense_Motive", "Spellcraft"]
Druid_Class_Less_Skills = ["Climb", "Craft", "Fly", "Heal", "Knowledge_Geography",
                     "Profession", "Ride", "Spellcraft", "Survival", "Swim"]
Fighter_Class_Less_Skills =["Craft", "Handle_Animal", "Intimidate", "Knowledge_Dungeoneering", 
                            "Knowledge_Engineering", "Profession", 
                            "Ride"]
Monk_Class_Less_Skills = ["Climb", "Craft", "Intimidate", "Knowledge_History", "Knowledge_Religion",
                     "Perform", "Profession", "Ride", "Sense_Motive", "Stealth", "Swim"]
Paladin_Class_Less_Skills = ["Craft", "Handle_Animal", "Heal",  "Knowledge_Religion", "Profession", "Ride", "Spellcraft"]
Ranger_Class_Less_Skills = ["Climb", "Craft", "Handle_Animal", "Heal", "Intimidate", "Knowledge_Dungeoneering", 
                       "Knowledge_Nature", "Profession", "Ride", "Spellcraft", "Stealth", "Swim"]
Rogue_Class_Less_Skills = ["Acrobatics", "Appraise", "Bluff", "Climb", "Craft", "Diplomacy", "Disguise", "Escape_Artist", 
                      "Intimidate", "Knowledge_Dungeoneering", "Knowledge_Local", "Linguistics", "Perception", 
                           "Perform", "Profession", 
                      "Sense_Motive", "Swim", "Use_Magic_Device"]
Sorcerer_Class_Less_Skills = ["Appraise", "Bluff", "Craft", "Fly", "Intimidate", "Profession"]
Wizard_Class_Less_Skills = ["Appraise", "Craft", "Knowledge_Dungeoneering", "Knowledge_Engineering", 
      "Knowledge_Geography", "Knowledge_History", "Knowledge_Local", "Knowledge_Nature", "Knowledge_Nobility", 
       "Knowledge_Planes", "Knowledge_Religion", "Linguistics", "Profession"]
### Setting Class Skill Bonus and Weighting for point disbursement
Class_Skill = 3
Important_Skills_Weighted = .6
Less_Important_Skills_Weighted = .4

Important_Skills_Total_Weighted = round(Skill_Ranks*Important_Skills_Weighted)
Less_Skill_Total_Weighted = round(Skill_Ranks*Less_Important_Skills_Weighted)

Wiz_Draw =[]
i = 0

## Setting the Functions for the randomized award of skill points, breaking into 60% and 40% baskets

def Top_Skills(i, Class, Weighted_Total, Class_Top_Skills, Draw):
    if Rand_Class == Class:
        while i < Important_Skills_Total_Weighted:
            x = Class_Top_Skills[random.randint(0,(len(Class_Top_Skills)-1))]
            Draw.append(x)
            i+=1
            
def Less_Skills(i, Class, Weighted_Total, Class_Less_Skills, Draw):
    if Rand_Class == Class:
        while i < Important_Skills_Total_Weighted:
            x = Class_Less_Skills[random.randint(0,(len(Class_Less_Skills)-1))]
            Draw.append(x)
            i+=1

def Populate_Skills(Skill_String, Draw, Skill_List, Class_Skill):
    if Skill_String in Draw:
        Skill_List = Skill_List + Class_Skill + Draw.count(Skill_String)
        print(Skill_String, Skill_List)
    else:
        print('Nuts!')

if 'Knowledge_Arcana' in Wiz_Draw:
      Knowledge_Arcana = Knowledge_Arcana + Class_Skill + Wiz_Draw.count('Knowledge_Arcana')
      print('Knowledge_Arcana:', Knowledge_Arcana)
 else:
      print('Nuts!')

## Function Calls

Less_Skills(i,"Wizard", Less_Skill_Total_Weighted,Wizard_Class_Less_Skills, Wiz_Draw)
Top_Skills(i,"Wizard", Important_Skills_Total_Weighted, Wizard_Class_Top_Skills, Wiz_Draw)

Populate_Skills('Knowledge_Local', Wiz_Draw, Knowledge_Local, Class_Skill)
Populate_Skills('Spellcraft', Wiz_Draw, Spellcraft, Class_Skill)
Populate_Skills('Fly', Wiz_Draw, Fly, Class_Skill)
Populate_Skills('Knowledge_Planes', Wiz_Draw, Knowledge_Planes, Class_Skill)
Populate_Skills('Acrobatics', Wiz_Draw, Acrobatics, Class_Skill)
Populate_Skills('Appraise', Wiz_Draw, Appraise, Class_Skill)
Populate_Skills('Bluff', Wiz_Draw, Bluff, Class_Skill)
Populate_Skills('Climb', Wiz_Draw, Climb, Class_Skill)
Populate_Skills('Craft', Wiz_Draw, Craft, Class_Skill)
Populate_Skills('Diplomacy', Wiz_Draw, Diplomacy, Class_Skill)
Populate_Skills('Disable_Device', Wiz_Draw, Disable_Device, Class_Skill)
Populate_Skills('Disguise', Wiz_Draw, Disguise, Class_Skill)
Populate_Skills('Escape_Artist', Wiz_Draw, Escape_Artist, Class_Skill)
Populate_Skills('Handle_Animal', Wiz_Draw, Handle_Animal, Class_Skill)
Populate_Skills('Heal', Wiz_Draw, Heal, Class_Skill)
Populate_Skills('Intimidate', Wiz_Draw, Intimidate, Class_Skill)
Populate_Skills('Knowledge_Arcana', Wiz_Draw, Knowledge_Arcana, Class_Skill)
Populate_Skills('Knowledge_Dungeoneering', Wiz_Draw, Knowledge_Dungeoneering, Class_Skill)
Populate_Skills('Knowledge_Engineering', Wiz_Draw, Knowledge_Engineering, Class_Skill)
Populate_Skills('Knowledge_Geography', Wiz_Draw, Knowledge_Geography, Class_Skill)
Populate_Skills('Knowledge_History', Wiz_Draw, Knowledge_History, Class_Skill)
Populate_Skills('Knowledge_Nature', Wiz_Draw, Knowledge_Nature, Class_Skill)
Populate_Skills('Knowledge_Nobility', Wiz_Draw, Knowledge_Nobility, Class_Skill)
Populate_Skills('Knowledge_Religion', Wiz_Draw, Knowledge_Dungeoneering, Class_Skill)
Populate_Skills('Linguistics', Wiz_Draw, Linguistics, Class_Skill)
Populate_Skills('Perception', Wiz_Draw, Perception, Class_Skill)
Populate_Skills('Perform', Wiz_Draw, Perform, Class_Skill)
Populate_Skills('Profession', Wiz_Draw, Profession, Class_Skill)
Populate_Skills('Ride', Wiz_Draw, Ride, Class_Skill)
Populate_Skills('Sense_Motive', Wiz_Draw, Sense_Motive, Class_Skill)
Populate_Skills('Sleight_of_Hand', Wiz_Draw, Sleight_of_Hand, Class_Skill)
Populate_Skills('Stealth', Wiz_Draw, Stealth, Class_Skill)
Populate_Skills('Survival', Wiz_Draw, Survival, Class_Skill)
Populate_Skills('Swim', Wiz_Draw, Swim, Class_Skill)
Populate_Skills('Use_Magic_Device', Wiz_Draw, Use_Magic_Device, Class_Skill)

Aucun commentaire:

Enregistrer un commentaire