omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    update and print list

    Pythonista
    2
    5
    92
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      resserone13v2.0 last edited by resserone13v2.0

      Im working on a game. its a remake of the nes game spy vs spy.

      when i print the self.items list its just giving me a memory loc. i made str method but im missing some thing. its hard to keep track of the player items without seeing the name and being able to update them.
      it currently prints like this

      ITEMS: <generator object Spy.__str__.<locals>.<genexpr> at 0x0000016C796456D0>
      
      class Game():
          def __init__(self):
              pass
      
          def move(self):
              pass
      
      
      class Room():
          def __init__(self, exit_to_plane, furniture, doors):
              self.exit_to_plane = False
              self.furniture = []
              self.doors = doors
      
      
      class Spy():
          def __init__(self):
              self.health = 100
              self.items = []
              self.able_to_fly = False
      
          def fly_airplane(self):
              if key not in self.items:
                  self.able_to_fly = True
      
          def pick_up_item(self, item):
              self.items.append(item)
      
          def remove_item(self, item):
              self.items.remove(item)
      
          def switch_weapons():
              pass
      
          def use_weapon(self, item):
              item.item_quantity -= 1
      
          def __str__(self):
              return (
                  f'HEALTH: {self.health}\n'
                  f'ITEMS: {print(item) for item in self.items}\n'
                  f'ABLE TO FLY: {self.able_to_fly}\n'
                  )
      
      
      class Item():
          def __init__(self, name, type, damage_amount, quantity):
              self.name = name
              self.type = type
              self.damage_amount = damage_amount
              self.item_quantity = quantity
      
          def __str__(self):
              return (
                  f'NAME: {self.name}\n'
                  f'TYPE: {self.type}\n'
                  f'DAMAGE AMOUNT: {self.damage_amount}\n'
                  f'item_quantity: {self.item_quantity}\n'
                  )
      
      
      # Items
      knife = Item('knife', 'weapon', 2, None)
      bomb = Item('bomb', 'weapon', 5, 2)
      oil_can = Item('oil can', 'weapon', 3, 1)
      key = Item('Key', 'key', 0, 1)
      
      print('ITEMS:')
      print(knife)
      print(bomb)
      print(oil_can)
      print(key)
      print()
      
      # Spies
      spy1 = Spy()
      spy2 = Spy()
      spy1.pick_up_item(Item('knife', 'weapon', 2, 1000))
      spy1.pick_up_item(key)
      spy1.fly_airplane()
      
      print('SPIES:')
      print('SPY 1')
      print(spy1)
      print('SPY 2')
      print(spy2)
      print()
      
      spy1.remove_item(key)
      spy1.fly_airplane()
      print(spy1)
      
      # Use an item
      
      print(bomb)
      
      
      
      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @resserone13v2.0 last edited by cvp

        @resserone13v2-0 see the + at end of each line in the return parameter. You have to return only one string

            def __str__(self):
                return (
                    f'NAME: {self.name}\n'+
                    f'TYPE: {self.type}\n'+
                    f'DAMAGE AMOUNT: {self.damage_amount}\n'+
                    f'item_quantity: {self.item_quantity}\n'
                    )
        

        Or only one f-string

            def __str__(self):
                return (
                    f'NAME: {self.name}\nTYPE: {self.type}\nDAMAGE AMOUNT: {self.damage_amount}\nitem_quantity: {self.item_quantity}\n'
                    )
        

        So you get

        ITEMS:
        NAME: knife
        TYPE: weapon
        DAMAGE AMOUNT: 2
        item_quantity: None
        
        NAME: bomb
        TYPE: weapon
        DAMAGE AMOUNT: 5
        item_quantity: 2
        
        NAME: oil can
        TYPE: weapon
        DAMAGE AMOUNT: 3
        item_quantity: 1
        
        NAME: Key
        TYPE: key
        DAMAGE AMOUNT: 0
        item_quantity: 1
        

        Still a problem with your

            def __str__(self):
                return (
                    f'HEALTH: {self.health}\n'+
                    f'ITEMS: {print(item) for item in self.items}\n'+
                    f'ABLE TO FLY: {self.able_to_fly}\n'
                    )
        

        Try

            def __str__(self):
                items = ''.join(str(item) for item in self.items)
                return (
                    f'HEALTH: {self.health}\n'+
                    f'ITEMS: {items}\n'+
                    f'ABLE TO FLY: {self.able_to_fly}\n'
                    )
        
        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @resserone13v2.0 last edited by

          @resserone13v2-0 Is the previous solution not ok for you?

          R 1 Reply Last reply Reply Quote 0
          • R
            resserone13v2.0 @cvp last edited by resserone13v2.0

            @cvp yes it worked perfect i used

            items = ''.join(str(item) for item in self.items)
            

            I did not add the + at the end of the f strings. it seems to be working fine.

            Thanks for your help.

            Ever since is been using Pythonista I've received lots of great help from the all of you in the forum.

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @resserone13v2.0 last edited by

              @resserone13v2-0 said

              Ever since is been using Pythonista I've received lots of great help from the all of you in the forum.

              Yes, marvelous forum for a marvelous app of a marvelous developer

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors