Home > Mobile >  Why am I getting this ValueError: too many values to unpack (expected 3)?
Why am I getting this ValueError: too many values to unpack (expected 3)?

Time:01-19

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)

CodePudding user response:

The function returns four variables...

return hours, minutes, seconds, remaining_seconds

... but then you only fetch three of those values:

hours, minutes, seconds = convert_seconds(5000)

CodePudding user response:

You need to include all values when unpacking:

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)

CodePudding user response:

Your function returns 4 values: hours, minutes, seconds, remaining_seconds

In your function call you are unpacking 3 values: hours, minutes, seconds

So you have too many values to unpack, the correct would be to unpack 4 values:

hours, minutes, seconds, remaining_seconds = convert_seconds(5000)

  • Related