Python помогите c кодом
Traceback (most recent call last):
File "C:/Users/dimal/OneDrive/Документы/NeftBot.py", line 27, in <module>
bot.polling(none_stop = True)
File "C:\Users\dimal\AppData\Local\Programs\Python\Python313\Lib\site-packages\telebot\__init__.py", line 1198, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "C:\Users\dimal\AppData\Local\Programs\Python\Python313\Lib\site-packages\telebot\__init__.py", line 1273, in __threaded_polling
raise e
File "C:\Users\dimal\AppData\Local\Programs\Python\Python313\Lib\site-packages\telebot\__init__.py", line 1235, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\dimal\AppData\Local\Programs\Python\Python313\Lib\site-packages\telebot\util.py", line 150, in raise_exceptions
raise self.exception_info
File "C:\Users\dimal\AppData\Local\Programs\Python\Python313\Lib\site-packages\telebot\util.py", line 93, in run
task(*args, **kwargs)
File "C:\Users\dimal\AppData\Local\Programs\Python\Python313\Lib\site-packages\telebot\__init__.py", line 9233, in _run_middlewares_and_handler
result = handler['function'](message)
File "C:/Users/dimal/OneDrive/Документы/NeftBot.py", line 22, in callback_worker
bot.send_message(call.message.chat.id,f"Сейчас нефть добывает: {Miner}\nСкорость добычи: 2/мин\nСейчас у вас нефти: {Oil} ")
UnboundLocalError: cannot access local variable 'Oil' where it is not associated with a value
Вот код:
import telebot
from telebot import types
import time
token = ''
bot = telebot.TeleBot(token)
Oil = 0
Miner = 'Шахтер'
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "/start":
main_keyboard = types.InlineKeyboardMarkup()
oil_btn = types.InlineKeyboardButton("Перейти к месту добычи", callback_data = 'oil')
main_keyboard.row(oil_btn)
bot.send_message(message.from_ user.id , "Добро пожаловать в игру 'Нефтянной магнат' сейчас вы находитесь в меню.", reply_markup = main_keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
if call.data == 'oil':
bot.delete_message(call.message.chat.id, call.message.message_id)
if Miner == 'Шахтер':
bot.send_message(call.message.chat.id,f"Сейчас нефть добывает: {Miner}\nСкорость добычи: 2/мин\nСейчас у вас нефти: {Oil} ")
while True:
Oil =+ 1
time.sleep(60)
bot.polling(none_stop = True)
У вас ошибка связана с использованием переменной Oil внутри функции callback_worker. Вот исправленный код:
import telebot
from telebot import types
import time
import threading
token = ''
bot = telebot.TeleBot(token)
Oil = 0
Miner = 'Шахтер'
oil_production_active = False
def oil_production():
global Oil, oil_production_active
while oil_production_active:
Oil += 2 # 2 единицы в минуту
time.sleep(60)
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "/start":
main_keyboard = types.InlineKeyboardMarkup()
oil_btn = types.InlineKeyboardButton("Перейти к месту добычи", callback_data = 'oil')
main_keyboard.row(oil_btn)
bot.send_message(message.from_user.id, "Добро пожаловать в игру 'Нефтянной магнат' сейчас вы находитесь в меню.", reply_markup = main_keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
global Oil, oil_production_active
if call.data == 'oil':
bot.delete_message(call.message.chat.id, call.message.message_id)
if Miner == 'Шахтер':
bot.send_message(call.message.chat.id,f"Сейчас нефть добывает: {Miner}\nСкорость добычи: 2/мин\nСейчас у вас нефти: {Oil} ")
if not oil_production_active:
oil_production_active = True
threading.Thread(target=oil_production, daemon=True).start()
bot.polling(none_stop = True)