Answer the question
In order to leave comments, you need to log in
What to fix so that callback.data is updated after each new button click?
Hello, I don’t understand why callback.data is not updated in my bot. After the first button press, only the action selected in the first pass is always performed, i.e. if I launched the bot and decided to add the numbers, no matter what I press further, it is the addition that is performed. Could you please point out my mistakes in the code?
def get_num(message):
global a
a = float(message.text)
question = 'Что будем с ним делать?'
keyboard = types.InlineKeyboardMarkup()
key_plus = types.InlineKeyboardButton(text='+', callback_data='plus')
key_minus = types.InlineKeyboardButton(text='-', callback_data='minus')
key_divide = types.InlineKeyboardButton(text='/', callback_data='divide')
key_multiply = types.InlineKeyboardButton(text='*', callback_data='multiply')
key_sqrt = types.InlineKeyboardButton(text='sqrt', callback_data='sqrt')
key_log = types.InlineKeyboardButton(text='log', callback_data='log')
key_sin = types.InlineKeyboardButton(text='sin', callback_data='sin')
key_cos = types.InlineKeyboardButton(text='cos', callback_data='cos')
keyboard.row(key_plus, key_minus, key_multiply, key_divide)
keyboard.row(key_sqrt, key_log, key_sin, key_cos)
bot.send_message(message.from_user.id, text=question, reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
mes = call.data
global a
global b
if mes == 'plus':
bot.send_message(call.message.chat.id, 'С чем будем складывать?')
@bot.message_handler(content_types=['text'])
def plus(message):
b = float(message.text)
b = a + b
str(b)
bot.send_message(call.message.chat.id, b)
elif mes == "minus":
bot.send_message(call.message.chat.id, 'Что будем вычитать?')
@bot.message_handler(content_types=['text'])
def minus(message):
b = float(message.text)
b = a - b
str(b)
bot.send_message(message.chat.id, b)
elif mes == 'divide':
bot.send_message(call.message.chat.id, 'На что будем делить?')
@bot.message_handler(content_types=['text'])
def divide(message):
b = float(message.text)
b = a / b
str(b)
bot.send_message(message.chat.id, b)
elif mes == 'multiply':
bot.send_message(call.message.chat.id, 'На что будем умножать?')
@bot.message_handler(content_types=['text'])
def multiply(message):
b = float(message.text)
b = a * b
str(b)
bot.send_message(message.chat.id, b)
elif mes == 'sqrt':
bot.send_message(call.message.chat.id, 'Корень какой степени вы хотите извлечь?')
@bot.message_handler(content_types=['text'])
def sqrt(message):
b = float(message.text)
b = math.pow(a, 1 / b)
str(b)
bot.send_message(message.chat.id, b)
elif mes == 'log':
bot.send_message(call.message.chat.id, 'Какое основание будет у логарифма?')
@bot.message_handler(content_types=['text'])
def log(message):
b = float(message.text)
b = math.log(a, b)
str(b)
bot.send_message(message.chat.id, b)
elif mes == 'sin':
a = math.radians(a)
b = math.sin(a)
b = str(b)
bot.send_message(call.message.chat.id, b)
elif mes == 'cos':
a = math.radians(a)
b = math.cos(a)
b = str(b)
bot.send_message(call.message.chat.id, b)
elif mes == 'float':
bot.send_message(call.message.chat.id,
'Введите нижний и верхний предел предел (два вещественных числа (пример - 5.43), через пробел)')
@bot.message_handler(content_types=['text'])
def get_lower(message):
b = message.text
c = b.split()
b = float(c[1])
a = float(c[0])
b = random.uniform(a, b)
bot.send_message(call.message.chat.id, str(b))
elif mes == 'int':
bot.send_message(call.message.chat.id, 'Введите нижний и верхний предел предел (два целых числа, через пробел)')
@bot.message_handler(content_types=['text'])
def get_lower(message):
b = message.text
c = b.split()
b = int(c[1])
a = int(c[0])
b = random.randint(a, b)
bot.send_message(call.message.chat.id, str(b))
else:
help()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question