所以我正在嘗試向一群人發送 i 訊息。我想知道如果“test1”(當前串列成員)出錯,我該如何跳轉到“test2”(下一個串列成員)。
profile_id = ['test1','test2']
for ids in profile_id:
api.send_direct_message(ids,text)
uj5u.com熱心網友回復:
for通過 iprofile_id 迭代。因此,變數ids將首先是元素'test 1',執行 for 回圈內的任何操作(即,向被呼叫的人發送訊息'test 1'。然后ids變為'test2',并向 發送訊息'test2'。但是您嘗試向人員串列發送訊息,而不是選擇的人 ( ids)。我假設該函式send_direct_message不允許串列,因此您需要將第三行設為api.send_direct_message(ids, text)。
uj5u.com熱心網友回復:
為什么不使用 TRY 然后 EXCEPT 用于相關類?
然后,您可以使用 PASS 什么都不做,繼續下一個。
uj5u.com熱心網友回復:
您可以嘗試使用try/except它,但僅在您raise遇到例外的情況下send_direct_message
profile_ids = ['test1','test2']
for index, profile_id in enumerate(profile_ids):
try:
api.send_direct_message(profile_id,text)
# assume that using just Exception is not a good tone, try to use specific exception you raise in the method here instead of just Exception
except Exception as exc:
if (index 1) >= len(profile_ids):
break
api.send_direct_message(profile_ids[index 1], text)
# but in such a case think of list index out of range error, that will be thrown when list will go to an end
uj5u.com熱心網友回復:
正如 Menashe 已經說過的,您可以使用 TRY、EXCEPT 和 PASS。像這樣:
profile_id = ['test1','test2']
for ids in profile_id:
try:
api.send_direct_message(ids,text)
except:
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450102.html
標籤:Python python-3.x 列表 推特 呸呸呸
上一篇:Python-將時區添加到時間戳
