S
S
ssofya_m2021-05-06 16:31:58
Telegram
ssofya_m, 2021-05-06 16:31:58

Given a string of a word and a translation. The bot asks for a word, and we have to write a translation. But the bot does not wait for our answer and moves on through the code.?

We have a line in which a word is written and a translation separated by a space, then the next, and so on. This piece of code is a mini-game, the bot has to send a word in the dialogue, and we need to write a translation. But he does not wait for our answer and immediately sends to the else branch. How can this be fixed?

def slovar(message):
    f =  open('read.txt', 'r+', encoding='utf-8') 
    fun = f.readline()
    fun = fun.split()
    while len(fun) != 0:
        sent = bot.send_message(message.chat.id, 'Переведи слово {word}'.format(word=fun[0]))
        bot.register_next_step_hundler(sent, slovar1)
        if sent.text == fun[1]:
            bot.send_message(message.chat.id, 'Верно')
        else: 
            bot.send_message(message.chat.id, 'Не верно! Правильный ответ: {word}'.format(word = fun[1]))
        fun = fun[2:]


6093efb538256046766866.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexa2007, 2021-05-06
@Alexa2007

# -*- coding: utf-8 -*-
import telebot

bot = telebot.TeleBot('11111111111')

en = ['one','two','three']
ru = ['один','два','три']

@bot.message_handler(commands=[ 'start'])
def send_welcome(message):
    msg = bot.send_message(message.chat.id,f'Переведи слово {ru[0]}')
    bot.register_next_step_handler(msg, process_name_step, 0 )


def process_name_step(message, count=0):
    print(message.text, count, len(en))
    if message.text == en[count]:
        if count<len(en)-1:
            msg = bot.send_message(message.chat.id,f'Молодец,\nПереведи слово {ru[count+1]}')
            bot.register_next_step_handler(msg, process_name_step, count+1)
        else:
            bot.send_message(message.chat.id,'Вопросы закончились')
            count=0
    else:
        if count<len(en)-1:
            msg = bot.send_message(message.chat.id,f'Неугадал,\nПереведи слово {ru[count+1]}')
            bot.register_next_step_handler(msg, process_name_step, count+1)
        else:
            bot.send_message(message.chat.id,'Вопросы закончились')
            count = 0
    

bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()

bot.polling()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question