【发布时间】:2022-01-11 18:16:00
【问题描述】:
我想简化这些“如果”,有哪些变体?谢谢。
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
【问题讨论】:
-
if user.profil not in ... -
你能不能把它写成代码?不太明白
-
至少,你可以写
if user.profil != '97' or user.profil != '82' or ...:。 -
(没有人写
if not ... == ...而不是if ... != ...。)
标签: python performance if-statement