Как при нажатии на кнопку прекратить цикл ? Telebot
Решил сделать игру в тг-боте. Как сделать чтобы при нажатии на кнопки "Стоп" прекращался цикл while и бот выводил сообщение? Код:
@bot.message_handler(content_types=['text'])
def sum_bet_func(message):
global sum_bet1
sum_bet1 = message.text
users_id = message.chat.id
balance1 = (sql.execute("SELECT * FROM users WHERE user_id = ?", (users_id,))).fetchone()[2]
if sum_bet1.isdigit():
if sum_bet1.isdigit() and int(sum_bet1) <= balance1:
bot.send_message(message.chat.id, text=f'💶 Ставка: {sum_bet1}₽\n\nИгра: Crash\nИгра начинается через 10 секунд...')
x = 1
while x <= 22:
if x == 11:
global factor
factor = random.uniform(1, 5)
stop_menu = types.InlineKeyboardMarkup()
stop = types.InlineKeyboardButton('СТОП-1', callback_data='stop')
stop_menu.add(stop)
bot.send_message(message.chat.id, text=f'💻 1/3 nn〽️ Множитель: {factor}', reply_markup=stop_menu)
if x == 16:
global factor2
factor2 = random.uniform(1, 5)
stop_menu2 = types.InlineKeyboardMarkup()
stop2 = types.InlineKeyboardButton('СТОП-2', callback_data='stop2')
stop_menu2.add(stop2)
bot.send_message(message.chat.id, text=f'💻 2/3 nn〽️ Множитель: {factor2}', reply_markup=stop_menu2)
if x == 21:
stop_menu3 = types.InlineKeyboardMarkup()
stop3 = types.InlineKeyboardButton('СТОП-3', callback_data='stop3')
stop_menu3.add(stop3)
global factor3
factor3 = random.uniform(1, 5)
bot.send_message(message.chat.id, text=f'💻 3/3 nn〽️ Множитель: {factor3}', reply_markup=stop_menu3)
if x == 22:
bot.send_message(message.chat.id, text=f'Ты проиграл')
break
time.sleep(1)
x += 1
else:
bot.send_message(message.chat.id, text=f'Недостаточно средств на балансе!')
Источник: Stack Overflow на русском