Math practice Telegram bot issue

I'm working on a school project. It's a Telegram bot for math practice. Right now I'm focusing on multiplication examples. The other operations will be similar.

Here's my code so far:

import telebot
import random
from telebot import types

bot = telebot.TeleBot('bot_token_here')

@bot.message_handler(commands=['start'])
def begin(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    multiply = types.KeyboardButton("*Multiply")
    divide = types.KeyboardButton("/Divide")
    add = types.KeyboardButton("+Add")
    subtract = types.KeyboardButton("-Subtract")
    keyboard.add(multiply, divide, add, subtract)
    bot.send_message(message.chat.id, "Hi! Pick a mode", reply_markup=keyboard)

@bot.message_handler(content_types=['text'])
def handle_messages(message):
    if message.text == "*Multiply":
        num1 = random.randint(1,10)
        num2 = random.randint(1,10)
        bot.send_message(message.chat.id, f"{num1} x {num2} =")
        if message.text == str(num1 * num2):
            bot.send_message(message.chat.id, "Great job! Correct!")

bot.polling(none_stop=True, interval=0)

I'm stuck on how to check the user's answer against the bot's question. My current approach isn't working. Any ideas?

ur code looks good, but the problem is in the handle_messages function. u need to use a separate handler for user answers. try using bot.register_next_step_handler() after sending the question. this will let u check the answer in a new function. good luck with ur project!

Hey there! I’ve worked on similar projects before, and I think I can help. The issue is that your handle_messages function is trying to do too much at once. You need to separate the question generation from the answer checking.

Here’s what I’d suggest:

  1. Create a separate function for generating and sending questions.
  2. Use bot.register_next_step_handler() to wait for the user’s answer.
  3. Implement another function to check the answer.

Also, consider using a dictionary to store the correct answers for each chat. This way, you can easily verify responses even if multiple users are using the bot simultaneously.

Don’t forget to add some error handling for non-numeric inputs. It’ll make your bot more robust.

Keep at it! You’re on the right track, and with a few tweaks, your bot will be up and running in no time.

I see the issue in your code. The problem lies in the handle_messages function. It’s not capturing the user’s answer correctly. You should implement a separate function to handle the user’s response after sending the question. Here’s a suggestion:

After sending the question, use bot.register_next_step_handler() to capture the next message from the user. This will allow you to process the answer in a dedicated function.

Also, consider adding a way to track the correct answer for each question. You could use a dictionary to store the expected answers for each chat ID.

Remember to handle potential errors, like non-numeric inputs. Good luck with your project!