Dec-11-2018, 10:51 AM
Hi all! Just a quick one as I'm new to Python and have square eyes after attempting to solve a simple issue after working with a java proj. of mine. Just wanted a second opinion.
Below you will see a working solution, it's pretty easy to see why I'd want to turn the value-change into a function!
Drchar
My mistake! I forgot you have to edit the global copy of the variable! How stupid of me.
RESOLVED FUNCTION
def ClassSet(x): if x == 1: bHitPoints = archerClass.hP bDmg = archerClass.Dmg bBlockChance = archerClass.bChance bBlockAmount = archerClass.DmgbPercent elif x == 2: bHitPoints = knightClass.hP bDmg = knightClass.Dmg bBlockChance = knightClass.bChance bBlockAmount = knightClass.DmgbPercent elif x == 3: bHitPoints = mageClass.hP bDmg = mageClass.Dmg bBlockChance = mageClass.bChance bBlockAmount = mageClass.DmgbPercent return bDmg, bHitPoints, bBlockChance, bBlockAmount
from GameMod.ClassChanges import ClassSet, UpWeapon from GameMod.ClassesAndMobs import archerClass, knightClass, mageClass from GameMod.Scenes import Scene1 from GameMod.weaponsNdefense import Bow, c, Sword, Wand, r, Shield, Robe, Armor global Dmg global HitPoints global bChance global bAmountPerc def ChangeValues(w,x,y,z): Dmg = w HitPoints = x bChance = y bAmountPerc = z Shield(c) Robe(c) Armor(c) Bow(c) Sword(c) Wand(c) archerClass() knightClass() mageClass() intro = open("Intro.txt") print(intro.read()) #THIS WILL BE SUBSET SOON! classChoice = int(input("1. Archer \n2. Knight \n3. Mage")) ChangeValues(ClassSet(classChoice)[0],ClassSet(classChoice)[1], \ ClassSet(classChoice)[2],ClassSet(classChoice)[3]) #SCENES #Set the wand to rare level if mage chosen. UpWeapon(classChoice,Scene1(HitPoints)[1],r,2) ChangeValues(ClassSet(classChoice)[0],ClassSet(classChoice)[1], \ ClassSet(classChoice)[2],ClassSet(classChoice)[3])This is my main class & the only associated function "ClassSet", I'm having issues with setting a variable-set via a main func. I'm not sure why it is, you can see the simple ChangeValues() function, it should just take the global variables and apply the given parameters to it (as shown further down the code). However it doesn't like it. Returning null & throwing exception. Is there any other solution to prevent me repeating 4 lines of code over and over again in replacement of the func. "ChangeValues".
Below you will see a working solution, it's pretty easy to see why I'd want to turn the value-change into a function!
global Dmg global HitPoints global bChance global bAmountPerc ''' def ChangeValues(w,x,y,z): Dmg = w HitPoints = x bChance = y bAmountPerc = z ''' Shield(c) Robe(c) Armor(c) Bow(c) Sword(c) Wand(c) archerClass() knightClass() mageClass() intro = open("Intro.txt") print(intro.read()) classChoice = int(input("1. Archer \n2. Knight \n3. Mage")) Dmg = ClassSet(classChoice)[0] HitPoints = ClassSet(classChoice)[1] bChance = ClassSet(classChoice)[2] bAmountPerc = ClassSet(classChoice)[3] '''ChangeValues(ClassSet(classChoice)[0],ClassSet(classChoice)[1], \ ClassSet(classChoice)[2],ClassSet(classChoice)[3])'''Thanks in advance,
Drchar
My mistake! I forgot you have to edit the global copy of the variable! How stupid of me.
RESOLVED FUNCTION
def ChangeValues(w,x,y,z): global Dmg global HitPoints global bChance global bAmountPerc Dmg = w HitPoints = x bChance = y bAmountPerc = z