简体   繁体   中英

How I can simplify this?

I want to simplify these "if's", what are the variants? Thanks.

    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

With not in you can do it:

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

So this checks if user.profil is not in the unwanteds set. {} makes it set so lookup is somewhat fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM