I was making a simple method that would return a list of a classes attribute values. It works fine but at the end of the list being printed there is always 'None' displayed as well. I am not sure why. Any clarification?
Here is my code: Player Class
class Player:
def __init__(self, name, caps, inventory, caravans):
self.name = name
self.caps = caps
self.inventory = inventory
self.caravans = caravans
Inventory Class and it's methods, I am referring to the list method.
class Inventory:
"""Items are stored by value not quantity"""
def __init__(self, weapons_ammo, meds_chems, armor):
self.weapons_ammo = weapons_ammo
self.meds_chems = meds_chems
self.armor = armor
def list(self):
"""List all inventory values."""
print(f'Weapons & Ammo: {self.weapons_ammo}\n'
f'Meds & Chems: {self.meds_chems}\n'
f'Armor: {self.armor}')
Instances
example_inventory = Inventory(5000, 2000, 1500)
example_player = Player('John', 10000, example_inventory, 3)
When I run this code:
print(example_player.inventory.list())
I get:
Weapons & Ammo: 5000 Meds & Chems: 2000 Armor: 1500 None
But when I run it without the print function:
example_player.inventory.list()
I get the proper return I was looking for with the 'None' at the end.
Could anyone explain why this is? I'm assuming it has to do with the method already calling the print function.
Thank you!
.list()only returnsNone, it doesn't return anything else. It prints something, though.