Reputation: 35
I have code that looks like this:
for item in items:
if item.data["position"] == '5':
item.delete()
continue
elif item.data["lang"] == 'SPA':
message_body = my_message_spa[item.data["position"]]
else:
message_body = my_message_eng[item.data["position"]] # English (default)
message = client.messages.create(
item.data["phone_number"],
body=message_body,
from_="xxxx"
)
# increment the position for this user
item.data["position"]+=1
This code is meant to send messages to an user.
Items is a list of dictionaries that has an user's phone number, date, language, and position.
Position refers to their position in the sequence of messages.
I would like to check the position of a user and if they have already received 5
messages, then it should be removed from the list and continue on to the next user. If the position of the user is <5
then it should go into the else if
statements and send messages according to the conditions met.
The list of dictionaries has the following structure:
{'phone_number': '+1234567890', 'position': 5, 'lang': 'ENG', 'Date': '2018-08-17 00:03:46'}
{'phone_number': '+0987654321', 'position': 2, 'lang': 'ENG', 'Date': '2018-12-18 07:10:47'}
The code worked fine previously and the new part I am adding/testing is the if statement checking for position but it seems like that if statement does not get invoked at all and goes straight to the below elif
checking for 'lang'
and sends the message and increments the position for the user. In my case, the user's position gets incremented to 6
.
Upvotes: 1
Views: 267
Reputation: 165
You seem to be comparing the value incorrectly. You are trying to compare a string "5" to an integer 5.
if item.data["position"] == '5':
note the quotes around 5.
For example:
print(1 == "1")
print(1 == 1)
returns False for the first one, and True for the second one.
You also should not delete items from something like a list when iterating over it. Instead try creating a copy or using list comprehension.
Upvotes: 3