Search code examples
pythonnonetype

How to not print "None"


for some reason I can't figure out why my code prints "None" after printing something out of a class or how to remove it. This is my code:

class armour:
    def __init__(self, name, defence):
        self.name = name
        self.defence = defence

    def block(self):
        print("your " + self.name + " blocked " + str(self.defence) + "\n")

class weapon:
    def __init__(self, name, attack):
        self.name = name
        self.attack = attack

    def attacks(self):
        print("your " + self.name + " blocked " + str(self.attack) + "\n")

loot = {
    "id1" : armour("test 1", 10),
    "id2" : weapon("test 2", 10),
}

equipment = {
    "armour" : loot["id" + str(1)],
    "weapon" : loot["id" + str(2)],
}


print(equipment["armour"].name)
print(equipment["armour"].block())
print(equipment["weapon"].name)
print(equipment["weapon"].attacks())

If anyone could also explain why it is happening it would be appreciated.


Solution

  • It print None because the function call inside the print statement does not return anything. Just make function calls and remove the print for them. Or return strings. For example:

    print(equipment["armour"].name)
    equipment["armour"].block()
    print(equipment["weapon"].name)
    equipment["weapon"].attacks()