首页 > 解决方案 > 有没有其他方法可以检查 args[1] 是否存在 + 可以在 days_array 中找到而不是尝试除?

问题描述

有没有其他方法可以检查 args[1] 是否存在 + 可以在 days_array 中找到而不是尝试除?

def homework(update: Update, context: CallbackContext) -> None:
    cid = update.message.chat_id
    u = users.get_user(cid)
    l_c = u["settings"]["language_code"]
    output_message = update.message.reply_text(tr.please_wait[l_c])
    if u["auth_data"]["logged_in"]:
        ekool_u = eKool.get_person_data(access_token=u["auth_data"]["access_token"])
        args = update.message.text.split(' ')
        content = f"{tr.tasks[l_c]} ({ekool_u['name1']}):\n"
        try:
            a = str(args[1]) in days_array
            content += msgc.todo(access_token=u["auth_data"]["access_token"], day=get_day_from_str(str(args[1])),student_id=ekool_u["roles"][0]["studentId"], lang_code=l_c)
        except:
            content += msgc.todo(access_token=u["auth_data"]["access_token"], student_id=ekool_u["roles"][0]["studentId"], lang_code=l_c)
        output_message.edit_text(content, parse_mode=telegram.constants.PARSEMODE_HTML)
    else:
        output_message.edit_text(tr.no_active_sessions[l_c])

标签: python-3.xtelegram-bot

解决方案


你的意思是像:

if str(args[1]) in days_array:
    content += msgc.todo(access_token=u["auth_data"]["access_token"], day=get_day_from_str(str(args[1])),student_id=ekool_u["roles"][0]["studentId"], lang_code=l_c)
else:
    content += msgc.todo(access_token=u["auth_data"]["access_token"], student_id=ekool_u["roles"][0]["studentId"], lang_code=l_c)
    

推荐阅读