我想简化这些"if's",变体是什么?谢谢。
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发布于 2021-12-06 14:37:16
使用not in,您可以这样做:
unwanteds = {'97', '82', '72', '64', '45', '25'}
if user.profil not in unwanteds:
markup = ...因此,这将检查user.profil是否不在unwanteds集合中。{}使其设置,所以查找有点快。
https://stackoverflow.com/questions/70247167
复制相似问题