Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:57:45+00:00 2026-06-17T20:57:45+00:00

This is just a question wondering why this doesn’t work. I have figured out

  • 0

This is just a question wondering why this doesn’t work. I have figured out a better way, but I don’t know why previously it wasn’t working.

global mydict
mydict = {}

This seems to work fine, and has made the mydict dictionary global. I even print mydict and it works. However, doing this:

global bool
bool = False

Does not seem to work. If trying to print bool in my code, I get:

UnboundLocalError: local variable 'bool' referenced before assignment

So why does it work for the dictionary and not the boolean?

Oh, also, if anyone was wondering how I figured out a better way, I initialised a class and made bool global in the class by doing: self.bool = False which worked. I got it from this question: Making all variables global


EDIT: As requested, I’ll post the necessary code:

import chatbot
global mydict
mydict = {}
global haveamessage
haveamessage = False

class MyBot(chatbot.ChatBot):
    def __init__(self, username, password, site):
        chatbot.ChatBot.__init__(self,username,password,site)

    def on_message(self, c, e):
        print mydict
        print haveamessage

if __name__ == '__main__':
    bot = MyBot("MyUsername", "MyPassword", "MySite")
    bot.start()

I’ll try explain this code. Pretty much the chatbot module is to allow users to create bots in wikis on Wikia, a company that allows wikis to be created which anyone can edit. On a wiki there is a chat extension where users can talk to. This script allows a bot to join the chat and do commands. on_message() goes off when someone posts something in the Chat.

So this prints:

{}
Traceback (most recent call last):
  File "file.py", line 146, in <module>
    bot.start()
  File "/Library/Python/2.7/site-packages/chatbot.py", line 371, in start
    self.on_message(self.c, e)
  File "file.py", line 12, in on_message
    print haveamessage
UnboundLocalError: local variable 'haveamessage' referenced before assignment

I’d like to clarify that the reason this isn’t producing an error for all of you is because you are not in a Wikia chat. the function on_message() only runs when someone posts something in the Chat. For example, I may have:

def on_message(self, c, e):
    if e.text == 'hello': # e.text is what a user posts in the chat. e = event
        c.send('Hello!') # c.send simply sends back a message in the chat. c = connection

So when someone posts in chat hello, the Bot posts back Hello!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:57:47+00:00Added an answer on June 17, 2026 at 8:57 pm

    The code you’ve posted does not produce the error you claim it does. However using the global keyword outside of a function has no effect, so it’s not surprising that it doesn’t work like you expect.

    I assume that in your real code, you’re actually trying to assign to haveamessage inside on_message. If so, you need a global statement inside that method.

    Basically the rule is: If you try to assign a global variable from within a function, you need to use the global keyword within that function. Otherwise you don’t need the global keyword at all. Whether or not the variable is a boolean makes no difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is just a Best Practices question, but I was wondering if
Ok, this might just be a dumb question, but I was wondering if Bluetooth
Simple question. Just wondering if this can be done without me having to enforce
This is just a trivial question of what convention you suggest. Recently, I have
This is just a curiosity question. I know that Spring can be used in
my apologies if this question doesn't make much sense, but I'm new(ish) to Actionscript,
I was just wondering if there was a way to use System.out.println(); or other
ok ... sort of have been dithering on posting this question but here goes:
I've already read this but it doesn't answer to my question. Here's my scenario:
This is more of a curiosity than a question, but I'm wondering why the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.