Python 3:如何链接两个不同类中的两个列表?

时间:2018-10-03 09:07:01

标签: python python-3.x list class

注意:这与重复的问题不同。重复的问题没有两个类和两个列表要链接。

如何将第二类中的一组列表分配给使用Python 3在第一类中创建的列表中的值?

我的输出应该是这样的:

Enter how many inventories: 3
Enter Inventory #1: fruits
Enter Inventory #2: veggies

然后询问每个库存中的物品

How many items in fruits: 2
Enter Item #1: apple
Enter Item #2: manggo

How many items in veggies: 3
Enter Item #1: cabbage
Enter Item #2: cucumber
Enter Item #3: eggplant

最终输出应如下所示:

fruits
apple manggo
veggies
cucumber cabbage

我的代码:

class Class_Inventory:
    maxcount_inventory = int(input("How many Inventories: "))
    inventory_name = []
    def __init__(self):
        for count_inventory in range(self.maxcount_inventory): 
            add_inventory = str(input("Enter Inventory #%d: " % (count_inventory+1)))
            self.inventory_name.append(add_inventory)

inventory_name = Class_Inventory().inventory_name
maxcount_inventory = Class_Inventory.maxcount_inventory

class Class_Item:
    item_name = []
    def __init__(self):
        for count_inv in inventory_name:
            self.maxcount_item = int(input("How many Items in %s: " % count_inv))
            for count_item in range(self.maxcount_item):
                add_item = str(input("Enter item #%d: " % (count_item+1)))                          
                self.item_name.append(add_item)   

item_name = Class_Item().item_name

for inv in inventory_name:
  print(inv)
  for item in item_name:
      print(item)

0 个答案:

没有答案