我想簡化這些“如果”,有哪些變體?謝謝。
user_id = message.from_user.id
user = user_data[user_id]
user.profil = message.text
if not user.profil == '97':
if not user.profil == '82':
if not user.profil == '72':
if not user.profil == '64':
if not user.profil == '45':
if not user.profil == '25':
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('97', '82', '72', '64', '45', '25')
msg = bot.send_message(message.chat.id, '??? ??? ??????.', reply_markup=markup)
bot.register_next_step_handler(msg, process_profil_step)
return
uj5u.com熱心網友回復:
有了not in你可以做到這一點:
unwanteds = {'97', '82', '72', '64', '45', '25'}
if user.profil not in unwanteds:
markup = ...
所以這會檢查 user.profil 是否不在不需要的集合中。{} 使其設定,因此查找有點快。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/377309.html
下一篇:提高讀取字串的函式速度
