I have a problem with mine Python 3 program. I use Mac OS X. This code is running properly.

You are watching: Not enough values to unpack

# -*- coding: utf-8 -*-#! python3# sendDuesReminders.py - sends emails based on payment status in spreadsheet.import openpyxl, smtplib, sys# open up the spreadsheet and get the recent dues status.wb = openpyxl.load_workbook("duesRecords.xlsx")sheet = wb.get_sheet_by_name("Sheet1")lastCol = sheet.max_columnlatestMonth = sheet.cell(row=1, column=lastCol).value# check each member"s payment status.unpaidMembers = for r in range(2, sheet.max_row + 1):payment = sheet.cell(row=r, column=lastCol).valueif payment != "zaplacone": surname = sheet.cell(row=r, column=2).value lastname = sheet.cell(row=r, column=3).value email = sheet.cell(row=r, column=4).value unpaidMembers = email# log in to email account.smtpObj = smtplib.SMTP_SSL("smtp.gmail.com", 465)smtpObj.ehlo()smtpObj.login("abc
abc.com", "1234")# Send the end reminder emails.for name, email in unpaidMembers.items()body = "Subject: %s - przypomnienie o platnosci raty za treningi GIT Parkour. " \ "\n\nPrzypominamy o uregulowaniu wplaty za uczestnictwo: %s w treningach GIT Parkour w ." \ "\n\nRecords show that you have not paid dues for %s. Please do " \ "this payment as shortly as possible."%(latestMonth, name, latestMonth)print("Sending email to %s..." % email)sendmailStatus = smtpObj.sendmail("abc
abc.com", email, body)if sendmailStatus != : print("There to be a difficulty sending email to %s: %s" % (email, sendmailStatus))smtpObj.quit()enter code hereProblems starts once I to be trying to add next value to the because that loop.

# Send out reminder emails.for name, lastname, email in unpaidMembers.items()body = "Subject: %s - przypomnienie o platnosci raty za treningi GIT Parkour. " \ "\n\nPrzypominamy o uregulowaniu wplaty za uczestnictwo: %s %s w treningach GIT Parkour w ." \ "\n\nRecords show that you have actually not paid dues because that %s. Please do " \ "this payment as quickly as possible."%(latestMonth, name, lastname, latestMonth)print("Sending email to %s..." % email)sendmailStatus = smtpObj.sendmail("abc
abc.com", email, body)Terminal reflects error:

Traceback (most recent call last): document "sendDuesEmailReminder.py", line 44, in for name, email, lastname in unpaidMembers.items():ValueError: not enough values come unpack (expected 3, acquired 2)
python python-3.x
share
improve this question
monitor
edited Feb 15 "17 in ~ 20:18

*

TankorSmash
11.3k55 yellow badges5757 silver badges9797 bronze badges
request Feb 15 "17 at 20:15
*

Jakub KłosJakub Kłos
16911 gold badge11 silver badge55 bronze badges
3
add a comment |

5 answer 5


active oldest Votes
7
You more than likely want to entrust the lastname you are analysis out here

lastname = sheet.cell(row=r, column=3).valueto something; at this time the program simply forgets it

you could do that two lines after, choose so

unpaidMembers = lastname, emailyour routine will still crash at the exact same place, due to the fact that .items() tho won"t provide you 3-tuples yet rather miscellaneous that has actually this structure: (name, (lastname, email))

good news is, python deserve to handle this

for name, (lastname, email) in unpaidMembers.items():etc.


re-superstructure
boost this prize
follow
reply Feb 15 "17 at 20:27
*

Paul PanzerPaul Panzer
48.2k22 gold badges3838 silver- badges8484 bronze title
include a comment |
3
In this line:

for name, email, lastname in unpaidMembers.items():unpaidMembers.items() must have actually only two values every iteration.

Here is a small example to highlight the problem:

This will work:

for alpha, beta, delta in <("first", "second", "third")>: print("alpha:", alpha, "beta:", beta, "delta:", delta)This will certainly fail, and also is what your code does:

for alpha, beta, delta in <("first", "second")>: print("alpha:", alpha, "beta:", beta, "delta:", delta)In this critical example, what worth in the perform is assigned to delta? Nothing, there aren"t sufficient values, and also that is the problem.


re-publishing
improve this prize
monitor
answer Feb 15 "17 in ~ 20:26
*

xandermonkeyxandermonkey
2,99422 gold badges2323 silver- badges4242 bronze badges
add a comment |
2
1. First should understand the error meaning

Error not sufficient values come unpack (expected 3, got 2) means:

a 2 part tuple, yet assign come 3 values

and I have actually written demo code to present for you:

#!/usr/bin/python# -*- coding: utf-8 -*-# Function: Showing exactly how to recognize ValueError "not enough values to unpack (expected 3, obtained 2)"# Author: Crifan Li# Update: 20191212def notEnoughUnpack(): """Showing exactly how to know python error `not sufficient values come unpack (expected 3, got 2)`""" # a dict, which solitary key"s value is two part tuple valueIsTwoPartTupleDict = "name1": ("lastname1", "email1"), "name2": ("lastname2", "email2"), # Test situation 1: acquired value from crucial gotLastname, gotEmail = valueIsTwoPartTupleDict<"name1"> # ok print("gotLastname=%s, gotEmail=%s" % (gotLastname, gotEmail)) # gotLastname, gotEmail, gotOtherSomeValue = valueIsTwoPartTupleDict<"name1"> # -> ValueError not sufficient values come unpack (expected 3, obtained 2) # Test situation 2: acquired from dict.items() because that eachKey, eachValues in valueIsTwoPartTupleDict.items(): print("eachKey=%s, eachValues=%s" % (eachKey, eachValues)) # very same as following: # elevator knowledge: every of dict.items() return (key, values) # here above eachValues is a tuple that two components for eachKey, (eachValuePart1, eachValuePart2) in valueIsTwoPartTupleDict.items(): print("eachKey=%s, eachValuePart1=%s, eachValuePart2=%s" % (eachKey, eachValuePart1, eachValuePart2)) # however following: because that eachKey, (eachValuePart1, eachValuePart2, eachValuePart3) in valueIsTwoPartTupleDict.items(): # will certainly -> ValueError not enough values to unpack (expected 3, acquired 2) passif __name__ == "__main__": notEnoughUnpack()using VSCode debug effect:

*

2. For your code

for name, email, lastname in unpaidMembers.items():but errorValueError: not enough values to unpack (expected 3, obtained 2)

means every item(a tuple value) in unpaidMembers, only have 1 parts:email, which corresponding over code

unpaidMembers = emailso should readjust code to:

for name, email in unpaidMembers.items():to protect against error.

See more: How Many Cups Is 400 Ml Of Milk? Convert 400 Milliliters To Cups

But clear you suppose extra lastname, therefore should adjust your over code to

unpaidMembers = (email, lastname)and far better change to better syntax:

for name, (email, lastname) in unpaidMembers.items():then everything is OK and also clear.