I'm getting this value error
ValueError: need more than 2 values to unpack)
and I don't know what it means.
Here is my code:
contact_map = {'Dennis Jones': ('989-123-4567', '[email protected]'), 'Susan': ('517-345-1234', '[email protected]'), 'Miller, Matthew': ('616-765-4321', '[email protected]')}
FORM = "{:<s};{:<d};{:<s}"
out_file = input("Enter a name for the output file: ")
output_file= open(out_file, "w")
for name, phone, email in contact_map.items():
output_file.write(FORM.format(name, phone, email))
output_file.close()
There must be two present when invoking dict.items() key and value. After that you need to unpack the value part in-order to get phone and email.
for name, value in contact_map.items():
phone = value[0]
email = value[1]
output_file.write(FORM.format(name, phone, email))
You're getting the error because you're attempting to unpack a tuple of len==3 (name, phone, email) but the items() returns (key, value), where value in this case is a tup of length 2.
You can unpack this in one line like:
for name, (phone, email) in contact_map.items():
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With