Как сделать проверку на ответ примера в Telegram боте?
Пишу Telegram бота на библиотеке pyTelegramBotAPI, возникла проблема с проверкой ответа пользователя на математический пример
@bot.message_handler(content_types=['text'])
def user_message(message):
if message.chat.type == 'private':
if message.text == '? Start':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=5)
addition = types.KeyboardButton('➕ Addition')
subtraction = types.KeyboardButton('➖ Subtraction')
multiplication = types.KeyboardButton('✖ Multiplication')
division = types.KeyboardButton('➗ Division')
markup.add(addition, subtraction, multiplication, division)
bot.send_message(message.chat.id, f'Welcome!', parse_mode='html', reply_markup=markup)
elif message.text == '➕ Addition':
num1 = (int(random.randint(1, 10)))
num2 = (int(random.randint(1, 10)))
msg = bot.send_message(message.chat.id, f'Solve the example:\n{int(num1)} + {int(num2)}', parse_mode='html')
bot.register_next_step_handler(msg, math_operation)
def math_operation(message):
num1 = (int(random.randint(1, 10)))
num2 = (int(random.randint(1, 10)))
answer = (int(num1)) + (int(num2))
if message.text == answer:
bot.send_message(message.chat.id, f'The example is solved! ?')
else:
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
example_again = types.KeyboardButton('Solve the example again')
back = types.KeyboardButton('? Back')
markup.add(example_again, back)
bot.send_message(message.chat.id, f'incorrect', parse_mode='html', reply_markup=markup)
Какой бы не был ответ от пользователя на пример, верный или неверный, то всегда выводит значение 'incorrect' из else. Необходимо чтобы на правильный ответ на пример выводило значение 'The example is solved!', а на неправильный 'incorrect'
Ответы (1 шт):
Автор решения: Владимир Антонов
→ Ссылка
Ты зачем то переприсваиваешь значения num1 и num2, они у тебя были рандомными, а ты их вместо того чтобы передать в следующую функцию создал заново Дальше нужно будет воткнуть обработчик ошибок на случай если тебе пришлют вообще не число
@bot.message_handler(content_types=['text'])
def user_message(message):
if message.chat.type == 'private':
if message.text == '? Start':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=5)
addition = types.KeyboardButton('➕ Addition')
subtraction = types.KeyboardButton('➖ Subtraction')
multiplication = types.KeyboardButton('✖ Multiplication')
division = types.KeyboardButton('➗ Division')
markup.add(addition, subtraction, multiplication, division)
bot.send_message(message.chat.id, f'Welcome!', parse_mode='html', reply_markup=markup)
elif message.text == '➕ Addition':
num1 = (int(random.randint(1, 10)))
num2 = (int(random.randint(1, 10)))
msg = bot.send_message(message.chat.id, f'Solve the example:\n{int(num1)} + {int(num2)}', parse_mode='html')
answer = num1 + num2
args = (answer, )
bot.register_next_step_handler(msg, math_operation, args=args)
def math_operation(message, args):
answer = args[0]
if int(message.text) == int(answer):
bot.send_message(message.chat.id, f'The example is solved! ?')
else:
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
example_again = types.KeyboardButton('Solve the example again')
back = types.KeyboardButton('? Back')
markup.add(example_again, back)
bot.send_message(message.chat.id, f'incorrect', parse_mode='html', reply_markup=markup)