Если коротко, то была у тебя логическая ошибка. Попробуй запуститься в этой версией:
import telebot
# Создаем экземпляр бота
bot = telebot.TeleBot('токен')
# Создаем список вопросов и ответов
questions = [
{
'question': 'Кто написал роман "Война и мир"?',
'answers': ['Лев Толстой', 'Федор Достоевский', 'Антон Чехов', 'Александр Пушкин'],
'correct_answer': 'Лев Толстой'
},
{
'question': 'Какой из этих языков является самым распространенным в мире?',
'answers': ['Английский', 'Испанский', 'Китайский', 'Хинди'],
'correct_answer': 'Английский'
},
{
'question': 'Какое животное называют "кораблем пустыни"?',
'answers': ['Верблюд', 'Слон', 'Лошадь', 'Осел'],
'correct_answer': 'Верблюд'
},
]
correct_answers = 0
question_index = 0
answered_questions = set()
@bot.message_handler(content_types=['text'])
def handle_message(message):
global correct_answers, question_index, answered_questions
if message.text == '/start':
question_index = 0
answered_questions = set()
send_question(message, question_index)
else:
try:
user_answer = message.text
if question_index not in answered_questions:
if user_answer == questions[question_index]['correct_answer']:
correct_answers += 1
answered_questions.add(question_index)
if question_index < len(questions) - 1:
question_index += 1
send_question(message, question_index)
else:
percentage = correct_answers / len(questions) * 100
bot.send_message(message.chat.id, f"Вы ответили правильно на {percentage:.2f}% вопросов.")
question_index = 0
except IndexError:
bot.send_message(message.chat.id, "Произошла ошибка при выборе вопроса.")
def send_question(message, index):
question = questions[index]
answer_options = '\n'.join([f'{i + 1}. {answer}' for i, answer in enumerate(question['answers'])])
message_text = f"{index + 1}. {question['question']}\n{answer_options}"
bot.send_message(message.chat.id, message_text)
bot.polling()