简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM