亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

所以,基本上我的代碼在打印我希望它打印的語句后打印 None 。我怎樣才能阻止這個 None 打印

所以,基本上我的代碼在打印我希望它打印的語句后打印 None 。我怎樣才能阻止這個 None 打印

嚕嚕噠 2023-07-11 15:31:51
所以,基本上我的代碼在打印我希望它打印的語句后打印 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())
查看完整描述

2 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

該print函數在 Python 中不返回,它只是寫入stdout; 因此,當您調用sleep實例的方法時,它只會打印None.


要解決這個問題,您要做的就是要么return不打印sleep,要么直接調用它而不將其包含在print語句中。


結果會是這樣的,例如:


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

或者


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


查看完整回答
反對 回復 2023-07-11
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

所以第一個打印語句是由于方法print中的函數造成的sleep。正在None按您的方式打印print(panda1.sleep())。該sleep方法不返回任何內容,因此None.

要擺脫None,您可以簡單地使用panda1.sleep()而不是print(panda1.sleep())。

但是,更好的選擇可能是返回您希望函數打印的消息sleep,然后使用print(panda1.sleep())


查看完整回答
反對 回復 2023-07-11
  • 2 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號