python 为什么我会得到这个ValueError:要解包的值太多(应该是3个)?

pcww981p  于 2023-01-19  发布在  Python
关注(0)|答案(3)|浏览(100)
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)
t1rydlwq

t1rydlwq1#

函数返回四个变量...

return hours, minutes, seconds, remaining_seconds

...但您只获取其中三个值:

hours, minutes, seconds = convert_seconds(5000)
jk9hmnmh

jk9hmnmh2#

解包时需要包括所有值:

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

k10s72fa3#

您的函数返回4个值:小时、分钟、秒、剩余_秒
在您的函数调用中,您正在解包3个值:小时、分钟、秒
因此,您需要解包的值太多,正确的方法是解包4个值:
hours, minutes, seconds, remaining_seconds = convert_seconds(5000)

相关问题