-3

I'm a newbie in python. I'm currently creating a simple program. Can you guys educate me how global keyword works? I ran the code and after typing 3 to select the shutdown option I'm being greeted by local variable 'tools' referenced before assignment. It's been hours and I can't figure this out. Thanks for the help guys.

This is the error code:

You can see the code here. I don't want to put the entire code in here because it's too long.

JazzBLues
  • 11
  • 3
  • 2
    You don't need to post the entire code here, but you need to post a [mre]. – Barmar Nov 11 '21 at 01:03
  • 2
    You shouldn't define functions inside `if` blocks. Define the functions at top-level, and call them in the the `if` conditions. – Barmar Nov 11 '21 at 01:03
  • Welcome. Please take a look at [ask]. As @Barmar said, don't post the entire code, just what is relevant. – joseville Nov 11 '21 at 01:04
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/261592/. Please note that this is *not a discussion forum*; we *do not care* about your level of expertise (and could guess anyway); we *do* care about having a *researched, specific* question. This is not the place to learn about broad topics; that's what [tutorials](https://docs.python.org/3/tutorial/controlflow.html#defining-functions), [documentation](https://docs.python.org/3/reference/simple_stmts.html#global) and [search engines](https://duckduckgo.com/?q=python+global+keyword) are for. – Karl Knechtel Nov 11 '21 at 01:16

1 Answers1

0

It's because, at the very end of the program, you are calling tools(Line 165). However, because tools was defined inside of an if block that wasn't executed, that function doesn't exist. What you need to do is move the definition of tools outside of the if-statement to the top level so that it is can be executed anywhere.

2pichar
  • 1,348
  • 4
  • 17