Ошибка в Python в pygame
import pygame
import pygame as pg
import telebot
TOKEN = "..."
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def start_t(message):
bot.send_message(message.chat.id, "Привет!")
@bot.message_handler(content_types = ['audio'])
def music_c(message):
if message.text == 'Big Rocky - Freestyle [Drift]':
pygame.init()
song = pygame.mixer.Sound('Big Rocky - Freestyle [ Drift ]')
clock = pygame.time.Clock()
song.play()
while True:
clock.tick(50)
pygame.quit()
bot.polling()
ошибка:UnboundLocalError: local variable 'clock' referenced before assignment
Ответы (1 шт):
Автор решения: Kers
→ Ссылка
Переменная присваивается в if что может и не произойти, но откуда она берется тогда во while?
Переписал код по идее должно все работать.
import pygame
import pygame as pg
import telebot
@bot.message_handler(content_types = ['audio'])
def music_c(message):
clock = pygame.time.Clock() # добавлено
if message.text == 'Big Rocky - Freestyle [Drift]':
pygame.init()
song = pygame.mixer.Sound('Big Rocky - Freestyle [ Drift ]')
# Убрано
song.play()
while True:
clock.tick(50)
pygame.quit()