如何不打印“无" [英] 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.
推荐答案
打印 None 是因为 print 语句中的函数调用没有返回任何内容.只需进行函数调用并删除它们的打印即可.或者返回字符串.例如:
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()
这篇关于如何不打印“无"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文