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
-
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 thisITEMS: <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)
-
@resserone13v2-0 see the
+
at end of each line in the return parameter. You have to return only one stringdef __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' )
-
@resserone13v2-0 Is the previous solution not ok for you?
-
@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.
-
@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