So, basically my code is printing None after printing the statement I want it to print. How can I stop this None from printing

北战南征 提交于 2021-02-11 12:35:00

问题


So, basically my code is printing None after printing the statement I want it to print. How can I stop this None from printing

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())

回答1:


The print function doesn't return in Python, it just writes to stdout; so, when you call the sleep method for the instance, it just prints None.

To fix this, what you have to do is either return instead of printing in sleep or just call it without enclosing it in the print statement.

Result would be like this, for example:

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'
            return '{} sleeps {} hours daily and should have {}'.format(self.name,self.time,self.food)
        else:
            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())

or

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)
panda2.sleep(10)
panda1.sleep(4)
panda3.sleep()



回答2:


So the first print statement is due to the print function in the sleep method. The None is being printed as you have print(panda1.sleep()). The sleep method doesn't return anything, and hence the None.

To get rid of the None, you can simply use panda1.sleep() rather than print(panda1.sleep()).

However, a better alternative might be to return the message you want your sleep function to print, and then use print(panda1.sleep())



来源:https://stackoverflow.com/questions/63711545/so-basically-my-code-is-printing-none-after-printing-the-statement-i-want-it-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!