#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    3

    Function and List question


    Code:
    stat_pool = ["|"] * 30
    strength = []
    
    def allocateStat(att_name,attribute,validSet):
        print("You have",len(attribute),"stat points in",att_name,"and",len(stat_pool),"stat points in your Stat Pool.")
        allo_att = int(input("Please choose an option: "))
        while allo_att not in validSet:
            allo_att = int(input("Please choose a valid option: "))
            if allo_att in validSet:
                attribute = stat_pool[:allo_att] + attribute
                del stat_pool[:allo_att]
    
    if choose_allo == "1":
        allocateStat("Strength",strength,range(1,len(stat_pool) + 1))
    Hi guys I have an issue here and I can't figure it out. Basically what I am trying to do is take a set amount from stat_pool and place it into strength.

    I am trying to use allo_att to create an integer to create a slice to remove the "|" from stat_pool and then add what was taken from stat_pool and place it into strength.

    The issue I am having is attribute = stat_pool[:allo_att] + attribute is not updating strength. I am not sure what is wrong here, I have another program where this works.

    The other issue is the first appearance of allo_att is not triggering the if statment if allo_att in validSet:. So basically I have to have an invalid input for the integer to be accepted.

    I hope that makes sense. Any help would be greatly appreciated!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Some points before I share my example:

    * To use 'strength' as a global, you have to define it as such inside your function
    * Instead of representing stat_pool and strength with a list of pipes, it's much easier to just have an integer. When you need to print it, you can print that value as pipes.
    * Please specify if you're using python3 or python2. It seems that you are using the former.
    * For scalability, I suggest you use a map structure (or the like) for all your attributes. This makes it much easier to handle numerous attributes.

    Code:
    stat_pool = 30
    attributes_map = {'strength': 0}
    
    def allocateStat(att_name,attribute,validSet):
        global attributes_map
        global stat_pool
        print("You have", attribute, "stat points in", att_name, "and", stat_pool,"stat points in your Stat Pool.")
        allo_att = int(input("Please choose an option: "))
        while allo_att > stat_pool:
            allo_att = int(input("Please choose a valid option: "))
    
        attributes_map[attribute] += allo_att
        stat_pool -= allo_att
    
    choose_allo = "1"
    
    if choose_allo == "1":
        allocateStat("Strength",'strength', stat_pool)
    
    print("stat_pool = %s" % (['|'] * stat_pool))
    print("strength  = %s" % (['|'] * attributes_map['strength']))
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    3
    Thanks so much for the reply I copied your code I hope you don't mind for future reference but with this particular instance I was determined to use slicing and lists. It's for a challenge in my Python book.

    Also, why dictionaries? I appreciate any information!

IMN logo majestic logo threadwatch logo seochat tools logo