-1

I'm making a discord.py bot, and I'm trying to implement a command that will tell you how much time there is until the period ends. The code works by itself, but if I try to implement it into the bot I get this error:

NameError: name 'timee' is not defined

(The variable is called 'timee' since I also use the time.sleep() function later)

This is the code it has a problem with:

def setTime():
    global timee

    print(timee)

    if timee > 59 and timee < 100:
        timee -= 100
        timee += 60
    elif timee > 159 and timee < 200:
        timee -= 100
        timee += 60
    elif timee > 259 and timee < 300:
        timee -= 100
        timee += 60

I have the print(timee) there to test if it thinks it's defined or not, which it doesn't.

This is my code that calls the setTime function (and should also set timee)

if currentTime < start:
    timee = start - currentTime

    print(timee)

    setTime()
    doThing()

    await client.send_message(message.channel, content = "School hasn't started yet! It starts in %s:%s" % (hours, minutes))
    print("%s got the time left." % message.author)

Edit: The 'timee' variable is declared outside of the if statement and the function as well.

Edit 2: I tried what someone commented, (The comment is deleted now) which is doing def setTime(timee): instead of

def setTime():
    global timee

And that works. I don't know if this is inefficient or what, but it works. ALSO, this exact same code works if It's not in the discord bot.

toti08
  • 2,448
  • 5
  • 24
  • 36
Sophie Snoww
  • 33
  • 1
  • 6
  • https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Yohboy Sep 14 '18 at 11:32
  • Is the `setTime` def in the same file as the code which calls it? – PM 2Ring Sep 14 '18 at 11:34
  • @PM2Ring Yes, I forgot to include it, but outside of the function and the if statement it is declared. – Sophie Snoww Sep 14 '18 at 11:36
  • Sorry, I don't know quite what you mean. Python doesn't have declarations. But we need to know if both those code blocks are in tbe same file, or in separate files, and you're using `import` to connect them, somehow. – PM 2Ring Sep 14 '18 at 11:39
  • Also is that `if currentTime < start:` stuff inside a function, or is it in the global context, outside the functions? – PM 2Ring Sep 14 '18 at 11:43
  • @PM2Ring Oh I thought you were asking if 'timee' is set outside of what I showed. (it is) But yes, they are both in the same file. The `if currentTime < start:` isn't inside a function. – Sophie Snoww Sep 14 '18 at 11:43
  • Try to reduce your problem to a complete and minimal example, that can be copy-pasted and gives the reported error. – FlyingTeller Sep 14 '18 at 11:44
  • Please don't forget to add a WORKING EXAMPLE. Most of the times just writing down a working example it is enough to answer a question. – Alex Poca Sep 14 '18 at 11:45
  • Ok. In that case, something weird is happening. ;) Make sure that the code you've posted is the same as what you're running. If `timee` is defined before you call `setTime` you shouldn't get a `NameError`. – PM 2Ring Sep 14 '18 at 11:47
  • @FlyingTeller I would, but this exact same code works if it isn't inside of the discord bot. If I just put it into its own file, it works fine. – Sophie Snoww Sep 14 '18 at 11:55

1 Answers1

0

Doing def setTime(timee): (as opposed to having def setTime(): and just trying to use the global variable) and then calling 'timee' from inside the function like that seems to fix everything.

If there is some more efficient or better way to do this then do tell, but for now I'll probably stick with this, since it's what I've found to work.

Sophie Snoww
  • 33
  • 1
  • 6
  • Do you expect the value of `timee` to be changed outside the function? Because then you will need to either `return timee` and capture the result, or use `global` – Patrick Haugh Sep 14 '18 at 12:26