为了账号安全,请及时绑定邮箱和手机立即绑定

我是 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 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号