I’m beginner in programming and have a problem in next code. the main problem is that when we use to command "math" the program gives you an example and you cant solve it, because program at once says that its uncorrect. How I can solve it? ive heard about about remembering the button status, but i dont know how to realize it.
import os
import telebot
from dotenv import load_dotenv
from mathematics import lvl1, lvl2, lvl3, lvl4, lvl1_str, lvl2_str, lvl3_str, lvl4_str
load_dotenv()
TELEGRAM_TOKEN = os.environ['TELEGRAM_TOKEN']
bot = telebot.TeleBot(TELEGRAM_TOKEN, parse_mode=None)
a = 1
money = 0
inv = "инвентарь"
help = "помощь"
fight = "начать битву"
math = "стать умным"
count = 0
dictionary = {
}
@bot.message_handler(content_types="text")
def message_all(message):
global money
global count
global dictionary
global lvl1, lvl2, lvl3, lvl4
global lvl1_str, lvl2_str, lvl3_str, lvl4_str
markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
if message.text == "/start":
markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
button_fight = telebot.types.KeyboardButton(fight)
button_inv = telebot.types.KeyboardButton(inv)
button_help = telebot.types.KeyboardButton(help)
button_math = telebot.types.KeyboardButton(math)
markup.row(button_fight, button_help)
markup.row(button_inv, button_math)
bot.send_message(message.chat.id, message.text, reply_markup=markup)
elif message.text == "помощь" or message.text == "/help":
bot.send_message(message.chat.id,
"/start - запустить ботa \n/help - высветить все команды \n/fight - начать сражение \n/inv - просмотреть инвентарь \n/math - решать задачи")
button_fight = telebot.types.KeyboardButton(fight)
button_inv = telebot.types.KeyboardButton(inv)
button_math = telebot.types.KeyboardButton(math)
markup.row(button_fight)
markup.row(button_inv, button_math)
bot.send_message(message.chat.id, message.text, reply_markup=markup)
elif message.text == "инвентарь" or message.text == "/inv":
bot.send_message(message.chat.id, "Ваши предметы:\n тут пока пусто...") # дописать
button_fight = telebot.types.KeyboardButton(fight)
button_help = telebot.types.KeyboardButton(help)
button_math = telebot.types.KeyboardButton(math)
markup.row(button_fight)
markup.row(button_help, button_math)
bot.send_message(message.chat.id, message.text, reply_markup=markup)
elif message.text == "стать умным" or message.text == "/math":
count += 5
if count < 11:
bot.send_message(message.chat.id, f"введите ответ на данный пример:\n {lvl1_str}")
if message.text == lvl1:
bot.send_message(message.chat.id,
'вы правильно ответили на данный пример\n ваш интеллект немного повысился\n +25 монет')
money += 25
else:
money -= 5
bot.send_message(message.chat.id,
'вы неверно ответили на данный пример\n -5 монет')
elif count < 21:
bot.send_message(message.chat.id, f"введите ответ на данный пример:\n {lvl2_str}")
if message.text == lvl2:
bot.send_message(message.chat.id,
'вы правильно ответили на данный пример\n ваш интеллект немного повысился\n +30 монет')
money += 30
else:
money -= 10
bot.send_message(message.chat.id,
'вы неверно ответили на данный пример\n -10 монет')
elif count < 31:
bot.send_message(message.chat.id, f"введите ответ на данный пример:\n {lvl3_str}")
if message.text == lvl3:
bot.send_message(message.chat.id,
'вы правильно ответили на данный пример\n ваш интеллект немного повысился\n +35 монет')
money += 35
else:
money -= 15
bot.send_message(message.chat.id,
'вы неверно ответили на данный пример\n -15 монет')
elif count < 41:
bot.send_message(message.chat.id, f"введите ответ на данный пример:\n {lvl4_str}")
if message.text == lvl4:
bot.send_message(message.chat.id,
'вы правильно ответили на данный пример\n ваш интеллект немного повысился\n +40 монет')
money += 40
else:
money -= 15
bot.send_message(message.chat.id,
'вы неверно ответили на данный пример\n -15 монет')
markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
button_fight = telebot.types.KeyboardButton(fight)
button_inv = telebot.types.KeyboardButton(inv)
button_help = telebot.types.KeyboardButton(help)
button_math = telebot.types.KeyboardButton(math)
markup.row(button_fight, button_math)
markup.row(button_inv, button_help)
bot.send_message(message.chat.id, message.text, reply_markup=markup)
elif count < 51 and message.text == "начать битву" or message.text == "/fight":
count = 0
bot.send_message(message.chat.id, "ищем противника...")
bot.send_message(message.chat.id, "противник найден!")
dictionary.update(enemy="противник найден!")
if "enemy" in dictionary:
markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
button_heal = telebot.types.KeyboardButton('лечение')
button_kick = telebot.types.KeyboardButton('удар')
button_superkick = telebot.types.KeyboardButton('суперудар')
markup.row(button_superkick)
markup.row(button_heal, button_kick)
bot.send_message(message.chat.id, message.text, reply_markup=markup)
dictionary.clear()
# здесь дописать код битвы и после ее окончания вернуть начальные кнопки и отправить поздравительное сообщение игроку
bot.infinity_polling()
file mathematics:
import random
from scipy.optimize import newton
m1 = random.randint(-100, 100)
m2 = random.randint(-100, 100)
m3 = random.randint(1, 10)
m4 = random.randint(1, 100)
m5 = random.randint(1, 100)
def spec(x):
return x ** 2 - m4
lvl4_unrounded = newton(spec, 2)
lvl1 = m1 + m2
lvl2 = m1 + m2 * m3
lvl3 = m3 ** 2 * m3 ** 3
lvl4 = round(lvl4_unrounded, 1)
lvl1_str = f'{m1} + {m2}'
lvl2_str = f'{m1} + {m2} * {m3}'
lvl3_str = f'{m3} ** 2 * {m3} ** 3'
lvl4_str = f'x ** 2 - {m4} = 0'
Ive tried to made another function and and call it in the right block, but it didnt work. ill be happy if someone can help me :)