所以,基本上我的代码在打印我希望它打印的语句后打印 None 。我怎样才能阻止这个 None 打印

所以,基本上我的代码在打印我希望它打印的语句后打印 None 。我怎样才能阻止这个 None 打印


class Panda:

    def __init__(self,name,gender,age):

        self.name=name

        self.gender=gender

        self.age=age

    def sleep(self,time=None):

        self.time=time

        if self.time!=None:

            if self.time>=3 and self.time<=5:

                self.food='Mixed Veggies'

            if self.time>=6 and self.time<=8:

                self.food='Eggplant & Tofu'

            if self.time>=9 and self.time<=11:

                self.food='Broccoli Chicken'

            print('{} sleeps {} hours daily and should have {}'.format(self.name,self.time,self.food))

        else:

            print("{}'s duration is unknown thus should have only bamboo leaves".format(self.name))


panda1=Panda("Kunfu","Male", 5)

panda2=Panda("Pan Pan","Female",3)

panda3=Panda("Ming Ming","Female",8)

print(panda2.sleep(10))

print(panda1.sleep(4))

print(panda3.sleep())


噜噜哒
浏览 102回答 2
2回答

慕斯709654

该print函数在 Python 中不返回,它只是写入stdout; 因此,当您调用sleep实例的方法时,它只会打印None.要解决这个问题,您要做的就是要么return不打印sleep,要么直接调用它而不将其包含在print语句中。结果会是这样的,例如:class Panda:&nbsp; &nbsp; def __init__(self,name,gender,age):&nbsp; &nbsp; &nbsp; &nbsp; self.name=name&nbsp; &nbsp; &nbsp; &nbsp; self.gender=gender&nbsp; &nbsp; &nbsp; &nbsp; self.age=age&nbsp; &nbsp; def sleep(self,time=None):&nbsp; &nbsp; &nbsp; &nbsp; self.time=time&nbsp; &nbsp; &nbsp; &nbsp; if self.time!=None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.time>=3 and self.time<=5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.food='Mixed Veggies'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.time>=6 and self.time<=8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.food='Eggplant & Tofu'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.time>=9 and self.time<=11:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.food='Broccoli Chicken'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '{} sleeps {} hours daily and should have {}'.format(self.name,self.time,self.food)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "{}'s duration is unknown thus should have only bamboo leaves".format(self.name)panda1=Panda("Kunfu","Male", 5)panda2=Panda("Pan Pan","Female",3)panda3=Panda("Ming Ming","Female",8)print(panda2.sleep(10))print(panda1.sleep(4))print(panda3.sleep())或者class Panda:&nbsp; &nbsp; def __init__(self,name,gender,age):&nbsp; &nbsp; &nbsp; &nbsp; self.name=name&nbsp; &nbsp; &nbsp; &nbsp; self.gender=gender&nbsp; &nbsp; &nbsp; &nbsp; self.age=age&nbsp; &nbsp; def sleep(self,time=None):&nbsp; &nbsp; &nbsp; &nbsp; self.time=time&nbsp; &nbsp; &nbsp; &nbsp; if self.time!=None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.time>=3 and self.time<=5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.food='Mixed Veggies'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.time>=6 and self.time<=8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.food='Eggplant & Tofu'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.time>=9 and self.time<=11:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.food='Broccoli Chicken'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('{} sleeps {} hours daily and should have {}'.format(self.name,self.time,self.food))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("{}'s duration is unknown thus should have only bamboo leaves".format(self.name))panda1=Panda("Kunfu","Male", 5)panda2=Panda("Pan Pan","Female",3)panda3=Panda("Ming Ming","Female",8)panda2.sleep(10)panda1.sleep(4)panda3.sleep()

蝴蝶不菲

所以第一个打印语句是由于方法print中的函数造成的sleep。正在None按您的方式打印print(panda1.sleep())。该sleep方法不返回任何内容,因此None.要摆脱None,您可以简单地使用panda1.sleep()而不是print(panda1.sleep())。但是,更好的选择可能是返回您希望函数打印的消息sleep,然后使用print(panda1.sleep())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python