提问人:Osckiie 提问时间:1/19/2023 最后编辑:Axe319Osckiie 更新时间:1/19/2023 访问量:396
为什么我收到此 ValueError:太多值无法解压缩(预期为 3)?
Why am I getting this ValueError: too many values to unpack (expected 3)?
问:
def convert_seconds(seconds):
hours = seconds // 3600
minutes = (seconds - hours * 3600) // 60
remaining_seconds = seconds - hours * 3600 - minutes * 60
return hours, minutes, seconds, remaining_seconds
hours, minutes, seconds = convert_seconds(5000)
答:
0赞
Avad
1/19/2023
#1
解压缩时需要包含所有值:
def convert_seconds(seconds):
hours = seconds // 3600
minutes = (seconds - hours * 3600) // 60
remaining_seconds = seconds - hours * 3600 - minutes * 60
return hours, minutes, seconds, remaining_seconds
hours, minutes, seconds, remaining_seconds = convert_seconds(5000)
1赞
John Gordon
1/19/2023
#2
该函数返回四个变量...
return hours, minutes, seconds, remaining_seconds
...但是,您只获取其中的三个值:
hours, minutes, seconds = convert_seconds(5000)
评论
0赞
Osckiie
1/19/2023
我现在看到错误。正如你所说,我错误地在最后一行添加了一个额外的词。在那之后,我再也没有把remaining_seconds放过
0赞
Ludovic Beghin
1/19/2023
#3
您的函数返回 4 个值:小时、分钟、秒remaining_seconds
在函数调用中,您将解压缩 3 个值:小时、分钟、秒
因此,您有太多的值需要解压缩,正确的做法是解压缩 4 个值:
hours, minutes, seconds, remaining_seconds = convert_seconds(5000)
评论
return
return hours, minutes, seconds, remaining_seconds
hours, minutes, seconds = convert_seconds(5000)
remaining_seconds