I’m trying to create a short python 3 function that takes 2 classes of items and spits out an inventory list. One class is just plain objects (let’s call them ‘item’ class) and the other is an object that contains other objects (I’ve been calling them ‘containers’). The problem arises when I try to print every single object (both classes) in the inventory. This includes items inside of containers. It should look something like this:
You are carrying:
an apple
a basket, containing:
an orange
a glass bottle, containing:
a quantity of water
My current code does this manually with basically:
if(object.type=='container'):
for object1 in object.inventory:
print(object.name)
if(object1.type=='container'):
for object2 in object1:
print(object2.name)
and so on, which is obviously atrocious. How could I make this function theoretically limitless?
-I am new to python so simple explanations for solutions are always appreciated!-