Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function method problems
#1
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.
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
Reply
#2
Please don't use globals. You want to be clear about what is going on in your functions. You do that with parameters and return values. See the function tutorial on how to do that. For something like this I would say a class for the player would work best:

class Player(object):

    def __init__(self, classIndex):
        if x == 1:
            self.HitPoints = archerClass.hP  
            self.Dmg = archerClass.Dmg
            self.BlockChance = archerClass.bChance
            self.BlockAmount = archerClass.DmgbPercent
        elif x == 2:
            self.HitPoints = knightClass.hP 
            self.Dmg = knightClass.Dmg
            self.BlockChance = knightClass.bChance
            self.BlockAmount = knightClass.DmgbPercent
        elif x == 3:
            self.HitPoints = mageClass.hP  
            self.Dmg = mageClass.Dmg
            self.BlockChance = mageClass.bChance
            self.BlockAmount = mageClass.DmgbPercent

intro = open("Intro.txt")        
print(intro.read())
classChoice = int(input("1. Archer \n2. Knight \n3. Mage"))
player = Player(classChoice)
It looks like you might have classes for archer, knight, and mage. If so it might work even better to have a factory function that creates an instance of one of those classes as the player.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
at least you can refactor the __init__ as

    def __init__(self, classIndex):
        player_classes = {1:archerClass, 2:kingClass, 3:mageClass}
        player_class = playerClasses.get(classIndex)
        self.HitPoints = player_class.hP  
        self.Dmg = player_class.Dmg
        self.BlockChance = player_class.bChance
        self.BlockAmount =player_class.DmgbPercent
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to use type= as a function/method keyword argument Skaperen 9 1,776 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Building a method name in a function ffgth 9 3,132 Oct-19-2020, 01:21 PM
Last Post: buran
  function/method help myv5285 3 2,742 May-17-2020, 04:19 AM
Last Post: buran
  Money conversion - problems with lists and .format function fatherted99 1 1,783 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  Accessing method as function object ClassicalSoul 2 1,975 Feb-14-2020, 09:31 PM
Last Post: wavic
  function vs method prateekshaw 2 2,153 Nov-14-2019, 07:00 PM
Last Post: DeaD_EyE
  Problems with a def function profeteus 4 2,280 Oct-20-2019, 06:50 PM
Last Post: ichabod801
  I'm trying to figure out whether this is a method or function call 357mag 2 2,380 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  problems when running a signrawtransaction method wectri 1 2,205 Dec-04-2018, 04:53 PM
Last Post: Larz60+
  Understanding Method/Function robdhunter 2 2,611 Mar-10-2018, 11:57 PM
Last Post: robdhunter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020