Home > Net >  How to add an object as a field of another in python
How to add an object as a field of another in python

Time:06-02

I am trying to make the player able to equip an amulet. only problem is, I've made the amulet an object and I'd like the player.bracelet1 field to store an object... Which is a string in the shop system :( Shoul I just use lots of if loops? Or is there some kind of method? Here is the necessary code. THere might be some holes cause my code is very long and I tried just to put the essential:

shop_stock=stock_dict[shop_id]
        with open(shop_stock,'r') as f:
            content=f.readlines()
        items_on_sale=[]
        for i in range (0,3):
            items_on_sale.append(content[r.randint(0,len(content)-1)].strip())
    action=int(input('What would you like to buy? (1,2 or 3)'))
    item_bought=items_on_sale[action-1]
    item_price=price_dict[item_bought]
    player.money-=item_price
    action=input(f'Do you want to equip your new {item_bought}?')
    if action in yes_inputs:
        equip_item(item_bought)
        elif action in no_inputs:
        player.backpack.append(item_bought)        
       
    def equip_item(item) 
        action=int(input(f'Would you like to equip your {item} as a '\
                         'right wrist bracelet(1), a left wrist bracelet(2) or'\
                         'a necklace(3)?'))
            if action==1:
            
                    player.bracelet1=item
            elif action==2:
                player.bracelet2=item
               
            elif action == 3:
               player.necklace=item
    #just to debug and check it works        
    print(player.bracelet1)

So this is just a piece I cut out of my function, if there needs to be something else, please tell me :D PS: I didn't put it here but each amulet (Small/Medium/Large Strength/Defense/Health) has three fields so I cant really do without objets...

CodePudding user response:

So, i've found the solution. Th eproblem was not an unexpected output, but rather I didn't know how to do. Well I found a solution when I realised you can use objects in dictionaries, which makes it pretty self explanatory even tho its a bit tedious to write the entire dictionnary

  • Related