Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Apparently, python thinks I haven't defined variables UserInput, message and HelpEmbed.

Python
@client.event
async def on_message(message):
 UserInput=message.content.split(" ")
 
 if len(UserInput)==5:
  if UserInput[0]=="!tz" or str(610890196199800852) in UserInput[0]: 
    try: 
      UserTime1=UserInput[1].split(".")
      now=datetime.utcnow()
      Date1= datetime(now.year, now.month, now.day, int(UserTime1[0], int(UserTime1[1])))  
      UserTZ1=UserInput[2]
      UserTZ2=UserInput[4] 
      TZ1=getTZ1(Date1, UserTZ1, now)
      TZ2=getTZ1(Date1, UserTZ2, now)
      Date1=timezone(TZ1).localize(datetime(now.year, now.month, now.day, int(UserTime1[0]), int(UserTime1[1]))).astimezone(timezone(TZ2))
      await message.channel.send("```"+str((Date1))+"```")

    
    except: 
      await message.channel.send("Oops! Seems like the command syntax is wrong. Try again!")
      await message.channel.send(str(traceback.format_exc()))

#end time convert

@client.event
async def displayembed():
  HelpEmbed=discord.Embed(
    title="Help Panel",
    description="Test description",
    #colour=discord.Colour.white()
  )

if UserInput[0]=="!tz" and UserInput[1]=="help":
  await message.channel.send(embed=HelpEmbed)

#end help

def getTZ1(DateInput, User_aTZ1, now):
  for TZ1Time in TimeZones:
    ConvertedTime1=utc.localize(now).astimezone(timezone(TZ1Time))
    if ConvertedTime1.tzname() == User_aTZ1.upper():
      return TZ1Time
 
# end functions


What I have tried:

Errors occur after #end time convert. Variables UserInput and message have been used and before #end time convert and work just fine. I really can't figure this out, can someone help, please? (I'm new to programming)

Note: This code uses Discord and pytz APIs (in case you don't recognise some commands and attributes)
Posted
Updated 15-Aug-19 3:45am
v2
Comments
ZurdoDev 15-Aug-19 7:25am    
What's the error message?

It's my understanding that python does not require variable declaration.
Dave Kreskowiak 15-Aug-19 8:43am    
No. Python KNOWS you haven't defined the variables properly. YOU THINK you have.

This comes down to understanding "scope". Read: https://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html

message is a parameter to the on_message function. It it not available outside of that function.

UserInput is a local variable within the on_message function. It cannot be used outside of that function.

HelpEmbed is a local variable within the displayembed function. It cannot be used outside of that function.

NB: Whitespace is important. The if block after assigning HelpEmbed is not indented, so it is not part of that function.
2.1.7 Indentation[^]
 
Share this answer
 
Quote:
Undefined variables, even though they are explicitely defined above?

Looks like you need to learn how the "scope of variables" works in Python.
When a variable is defined inside a function definition, it exist only inside that function, and anything outside can't see it.
Scope - Python Tutorial[^]
Python Tutorial: Global vs. Local Variables and Namespaces[^]
 
Share this answer
 
See 9. Classes — Python 3.7.4 documentation#python-scopes-and-namespaces[^] for a full explanation of scopes in Python.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900