0

I am creating a shop/inventory system for a python rpg and return several variables from a function in my shop. I create 3-5 items every time the shop is visited so right now if you print one of my returned variables it will print the info from the last item created. I need to be able to store each unique items values in an array so that each item can be called on not just the last one.

if plus == 0: #if item made has a plus equal to nothing
                        if has_add == 'yes': #if it has an add
                            if item_type == 'weapon':  #if it is a weapon (no plus, has add, is weapon)   
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + " of " + wep_add + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': #if it is an armor (no plus, has add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece + " of " + arm_add + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this
                        else: # if item doesnt have add
                            if item_type == 'weapon':  #if it is a weapon (no plus, no add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': # if it is an armor (no plus, no add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece  + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this
                    else: #if item made has a plus 
                        if has_add == 'yes': # if it has an add
                            if item_type == 'weapon': # if it is a weapon (has plus, has add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + " of " + wep_add + " +" + str(plus) + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': #if it is an armor (has plus, has add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece + " of " + arm_add + " +" + str(plus)  + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this
                        else: # if it doesnt have an add
                            if item_type == 'weapon': #if it is a weapon (has plus, no add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + " +" +  str(plus) + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': #if it is an armor (has plus, no add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece + " +" + str(plus)  + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this

                    if item_type == 'weapon': #allows us to get info out of function
                        return (created_item, count, quality, wep_adj, weapon_type, wep_add, plus, weapon_attack, wep_price, randvar)
                    else:
                        return (created_item, count, quality, arm_adj, armor_piece, arm_add, plus, armor_defense, arm_price, armor_hp)



                while items_amount > items_made: #if we've made 3-5 items, stop making them
                    new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = make_item()
                    items_made += 1 #increase items made every time one is made
                    count += 1
                return (new_item, count, quality, adj, piece, add, plus, stat, price, other_stat)      



            new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = generate_items() #call function to make all items when shop is visited
            print(new_item, count, quality, adj, piece, add, plus, stat, price, other_stat)

Since my code is so immense I don't want to link the entirety of relevant code. The most relevant is this:

while items_amount > items_made: #if we've made 3-5 items, stop making them
                    new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = make_item()
                    items_made += 1 #increase items made every time one is made
                    count += 1
                return (new_item, count, quality, adj, piece, add, plus, stat, price, other_stat)

I need to be able to return them as arrays instead of variables

4
  • What you are doing already return a tuple containg the variables new_item, count, quality, adj, piece, add, plus, stat, price, other_stat Commented Apr 25, 2016 at 4:10
  • @user312016 yes but I need each variable in the tuple to be stored seperately for each item created. Right now each item created overwrites the last. Commented Apr 25, 2016 at 4:11
  • @Shnipper You may use a dictionnary instead though. What are you meaning by "Right now each item created overwrites the last" ? Commented Apr 25, 2016 at 4:13
  • @user312016 the function is called between 3-5 times. Every time the function is called a new item is created. In my last line of the first code above I print every variable that is returned. What gets printed is the information for the last item that was created. I need a way to access the information for the other items that were made before that one Commented Apr 25, 2016 at 4:16

2 Answers 2

2

You could treat whatever you are returning as one tuple variable and append it to a list:

items = list()
while items_amount > items_made:  # if we've made 3-5 items, stop making them
    new_item_details = make_item() # assign to one tuple variable
    items.append(new_item_details) # append to list
    items_made += 1  # increase items made every time one is made
    count += 1
return items

If you want to access each details of individual item and you want it to be nice as well, I suggest you create a class with required variables:

class Item(object):
    def __init__(self, item_details):
        self.item = item_details[0]
        self.count = item_details[1]
        self.quality = item_details[2]
        self.adj = item_details[3]

        ... # similarily other fields

        self.other_stat = item_details[9]

and then you can create these items:

items = list()
while items_amount > items_made:  # if we've made 3-5 items, stop making them
    new_item_details = make_item() # assign to one tuple variable
    items.append(Item(new_item_details)) # append to list
    items_made += 1  # increase items made every time one is made
    count += 1
return items

Now, if you want to access adj of 2nd item:

# 1st index would be the second item in the list 
items[1].adj # access using variable name on the instance
Sign up to request clarification or add additional context in comments.

Comments

1

Insert your tuples into a list:

items = list()
while items_amount > items_made:  # if we've made 3-5 items, stop making them
    new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = make_item()
    items_made += 1  # increase items made every time one is made
    count += 1
    items.append((new_item, count, quality, adj, piece, add, plus, stat, price, other_stat))

return items

6 Comments

TypeError: append() takes exactly one argument (10 given)
@Shniper I fixed a typo.
This works but how do I access each individual value. So if I wanted to access the adj of the 2nd item how would I? I tried items.adj[1], didn't work
You mean you added extra parenthesis to make the appended type a tuple.
@Shniper You would have to do items[1][3]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.