I’m developing a telegram bot using aiogram and asyncio for a math quiz game. The bot runs a series of questions in a loop, but I can’t seem to capture the user’s input after each question.
The handler begins the quiz and enters a loop to present math problems. I need to pause the loop and wait for the user’s response after asking each question to verify if their answer is correct. At the moment, I’m struggling with how to implement this pause for user input.
@router.message(F.text == "Start Quiz")
async def start_quiz(message: Message, state: FSMContext, user_answer = QuizQuestion.answer):
await state.set_state(QuizQuestion.answer)
correct_responses = []
score = 0
for question_number in range(1, 51):
number_one = random.randint(1, 15)
number_two = random.randint(1, 15)
expected_result = number_one + number_two
if expected_result not in correct_responses:
correct_responses.append(expected_result)
await message.answer(f'Question {question_number}: {number_one} + {number_two} = ?', reply_markup=keyboard.quiz_buttons)
if int(user_answer) == expected_result: # Waiting for user input here
score += 1
await message.reply(f"Correct! Your score: {score}")
else:
score -= 1
await message.reply(f"Incorrect! Your score: {score}")
@router.message(QuizQuestion.answer)
async def handle_response(message: Message, state: FSMContext):
await state.update_data(answer=message.text)
await message.answer(f"You entered: {message.text}")
user_answer = await state.get_state()
I keep encountering this error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'State'
Since this is my first bot project, I’m likely missing a fundamental concept. How do I appropriately wait for user input during the loop?