2016-04-25 61 views
0

我正在为一个python RPG创建一个商店/库存系统,并从我的商店中的一个函数返回多个变量。每次访问商店时,我都会创建3-5个项目,所以如果您打印一个返回的变量,它将打印最后创建的项目的信息。我需要能够将每个唯一的项目值存储在数组中,以便每个项目不仅可以在最后一个项目上调用。如何多次返回变量并创建数组

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) 

由于我的代码是如此之大,我不想链接整个相关的代码。最相关的是这样的:

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) 

我需要能够将其返回为数组来代替变量

+0

你在做什么已经返回一个元组方含变量NEW_ITEM,计数,q价值,other_stat – user312016

+0

@ user312016是的,但我需要为每个创建的项目单独存储元组中的每个变量。现在每个项目都会覆盖最后一个项目。 – Shniper

+0

@Shnipper虽然你可以使用一个dictionnary。 “现在创建的每个项目都会覆盖最后一个”是什么意思? – user312016

回答

1

插入你的元组到一个列表:

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 
+0

TypeError:append()只需要一个参数(给出10) – Shniper

+0

@Shniper我修正了一个错字。 – user312016

+0

这可行,但我如何访问每个单独的值。所以,如果我想访问第二个项目的形式,我会怎样?我试过items.adj [1],没有工作 – Shniper

2

你可以把任何你是返回为一个tuple变量并将其附加到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 

如果要访问个人item的每个细节,你希望它是不错的,以及,我建议你创建一个类必需的变量:

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] 

然后你就可以创建这些项目:

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 

现在,如果你要访问第2项的adj

# 1st index would be the second item in the list 
items[1].adj # access using variable name on the instance