簡體   English   中英

我怎樣才能簡化這個?

[英]How I can simplify this?

我想簡化這些“如果”,有哪些變體? 謝謝。

    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

not in你可以這樣做:

unwanteds = {'97', '82', '72', '64', '45', '25'}
if user.profil not in unwanteds:
    markup = ...

所以這會檢查 user.profil 是否不在不需要的集合中。 {} 進行設置,因此查找速度有點快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM