Не получается отправить дробное число
Не получается отправить боту дробное число.
Код:
@bot.callback_query_handler(func=lambda call: True)
def callback(call):
if call.message:
if call.data == 'calculate':
bot.edit_message_text(text_1, call.message.chat.id, call.message.message_id)
bot.register_next_step_handler(call.message, value_1)
def value_1(message):
global val_1, val_2
val_1 = int(message.text)
bot.send_message(message.chat.id, text_2)
bot.register_next_step_handler(message, value_2)
def value_2(message):
global val_1, val_2
val_2 = int(message.text)
number_1 = val_1
number_2 = val_2
profit = (number_2 - number_1)
percent = (number_1 / 100)
total = '{:.2f}'.format(profit / percent)
total_val_1 = '{:.1f}'.format(val_1)
total_val_2 = '{:.1f}'.format(val_2)
bot.send_message(message.chat.id, f'? Результат расчета\n\nСумма покупки: {total_val_1}\nСумма продажи: {total_val_2}\n\nПроцент спреда равен: {total} %')
Когда я отправляю числа, они записываются в переменные val, но когда я отправляю дробное число, то бот зависает.
Ответы (1 шт):
Автор решения: oleksandrigo
→ Ссылка
Вы ведь все равно числа к дроби приводите, зачем int(message.text)?
from telebot import TeleBot, types
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
import config
bot = TeleBot(config.BOT_TOKEN)
val_1 = 0
val_2 = 0
text_1 = 'введите первое число'
text_2 = 'введите второе число'
@bot.message_handler(commands=['start'])
def send_welcome(message: types.Message):
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton('Кнопка', callback_data='calculate'))
bot.send_message(message.chat.id, "text", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data == 'calculate')
def callback(call: types.CallbackQuery):
bot.answer_callback_query(callback_query_id=call.id)
bot.edit_message_text(text_1, call.message.chat.id, call.message.message_id)
bot.register_next_step_handler(call.message, value_1)
def value_1(message: types.Message):
global val_1
val_1 = float(message.text)
bot.send_message(message.chat.id, text_2)
bot.register_next_step_handler(message, value_2)
def value_2(message: types.Message):
global val_1, val_2
val_2 = float(message.text)
number_1 = val_1
number_2 = val_2
profit = (number_2 - number_1)
percent = (number_1 / 100)
total = '{:.2f}'.format(profit / percent)
total_val_1 = '{:.1f}'.format(val_1)
total_val_2 = '{:.1f}'.format(val_2)
bot.send_message(message.chat.id,
f'? Результат расчета\n\n'
f'Сумма покупки: {total_val_1}\n'
f'Сумма продажи: {total_val_2}\n\n'
f'Процент спреда равен: {total} %')
bot.infinity_polling(skip_pending=True)
