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

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

我是 Python 中 OOP 的初學者,并且在我的代碼中得到“沒有足夠的值來解包”

我是 Python 中 OOP 的初學者,并且在我的代碼中得到“沒有足夠的值來解包”

料青山看我應如是 2022-05-19 14:08:57
class Fruits:    def __init__(self,name,colour,taste,hasColour):         self.name = name        self.colour = colour        self.taste = taste        self.hasColour = hasColour    def hasAlternateColour(self):        return self.hasColourif __name__== "__main__":    fruitList = []    print("Enter name, colour, taste of the fruit")    for itr in range(2):        name,colour,taste = input().split()        hasColour = input("Does it have another colour")        fruitList.append(Fruits(name,colour,taste,hasColour))    for fruit in fruitList:           print(fruit.name,fruit.colour,fruit.taste,fruit.hasAlternateColour(),sep="\t")輸出:Enter name, colour, taste of the fruitapple red sweetDoes it have another colour yes錯誤:> Traceback (most recent call last):   File> "E:/Programs/pyoop/Fruitclass.py", line 15, in <module>>     name,colour,taste = input().split() ValueError: not enough values to unpack (expected 3, got 0)
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

我的猜測是你已經按下Enter了循環的第二次迭代,只是因為程序沒有任何消息就卡住了。您可以替換print為input并將其放入循環中,因此程序將始終以消息停止Enter name, colour, taste of the fruit::


if __name__== "__main__":

    fruitList = []

    for itr in range(2):

        name, colour, taste = input("Enter name, colour, taste of the fruit ").split()

        hasColour = input("Does it have another colour ")

        fruitList.append(Fruits(name, colour, taste, hasColour))


查看完整回答
反對 回復 2022-05-19
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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