Dear James;
I modified the code as you said, but the output is the same as before. I mean it does not go to the next question and iterates first question. Do you have any idea helps me?
The total code is:
import telebot
bot = telebot.TeleBot('*************************************************************') # Replace with your bot token
# Questions and answers stored in a dictionary
questions = {
"What is the answer to question 1": "A",
"What is the answer to question 2": "B",
"What is the answer to question 3": "C"
}
# Initialize the current_question outside of the functions
current_question = None
# Start command handler
@bot.message_handler(commands=['start'])
def start_message(message):
global current_question
bot.reply_to(message, "Welcome to the Quiz Bot! Please answer the following questions:")
# Start the question answering loop
current_question=iter(questions)
current_question = next(current_question, None)
bot.send_message(message.chat.id, current_question)
# Answer handler
@bot.message_handler(func=lambda message: True)
def answer_question(message):
global current_question
# global questions # Access the global questions dictionary
# Check if the user's answer is correct for the current question
correct_answer = questions[current_question]
if message.text.lower() == correct_answer.lower():
bot.reply_to(message, "Correct!")
# Move on to the next question
current_question=iter(questions)
current_question = next(current_question, None)
if current_question:
bot.send_message(message.chat.id, current_question)
else:
bot.send_message(message.chat.id, "Congratulations! You have answered all the questions correctly.")
else:
bot.reply_to(message, "Incorrect. The answer is " + correct_answer + ".")
bot.polling()