S
S
SunnyWolf2020-06-21 14:41:10
Python
SunnyWolf, 2020-06-21 14:41:10

Error in code, how to fix?

Here is the code:

def RanSL(message):
      time.sleep(0.5)
      N = random.randint(1,5)
      Ne = (str(N)) + '.txt'
      file = open(Ne,"r")
      random = file.read()
      bot.send_message(message.chat.id,random)


mistake:
UnboundLocalError: local variable 'random' referenced before assignment


When I declare a variable not in a function, but at the beginning, it works. But I need it in a function.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VitiaKotik, 2020-06-21
@NEDOprogrammer

random is not initialized

S
SangelS, 2020-06-21
@SangelS

import time
import random

def RanSL(message):
    time.sleep(0.5)
    N = random.randint(1,5)
    Ne = (str(N)) + '.txt'
    file = open(Ne,"r")
    random = file.read()
bot.send_message(message.chat.id,random)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question