python-3.x - 有没有其他方法可以检查 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])
解决方案
你的意思是像:
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)
推荐阅读
- vue.js - 在页面加载时使用 Vuelidate 验证表单
- python - Python FastAPI 不保存来自 POST 文件请求的内容
- node.js - 根据条件添加自定义字段并在nodeJs中返回一个新集合
- reactjs - React Rainbow UI 示例缺少主题
- python - 将 defaultdict 存储到文本文件中
- scala - 理解用大括号创建的函数字面量是如何工作的
- mongodb - Spring Boot MongoDb 存储库 findAll() 返回空列表
- curl - 为什么 cURL 会为任何 Instagram 链接返回 302?
- kubernetes - 有什么办法给流量吗?
- tcp - Allen Bradley ControlLogix 通过 TCP 通信