Exercise: Structure a Script With a Name-Main Guard
Bartosz Zaczyński RP Team on July 5, 2026
@jurib It comes down to scope. When QUOTES sits at the top of the file, outside every function, it’s a module-level (global) name. Everything in the file can see it, and it’s still there as an attribute after the file is imported, so another script can reach it as solution.QUOTES.
When you move QUOTES inside main(), it becomes a local variable of that function. It only exists while main() is running, and nothing outside main() can see it, not other functions, not the top level of the module, and not code that imports the file. So a reference to QUOTES from outside the function raises NameError: name 'QUOTES' is not defined, and the exercise checker, which imports your file and looks for solution.QUOTES, no longer finds it and reports an error.
That ties back to the whole point of this exercise. The name-main guard is there so the file can be imported and reused, and the description specifically wants another script to be able to reuse the QUOTES list. That only works if QUOTES stays at module level. So the solution keeps QUOTES at the top and moves only the quote-picking and printing into main():
import random
QUOTES = [
"The only way to do great work is to love what you do.",
"In the middle of difficulty lies opportunity.",
"Stay hungry, stay foolish.",
]
def main():
quote = random.choice(QUOTES)
print(f"Quote of the day: {quote}")
if __name__ == "__main__":
main()
If you want to go deeper on how Python decides which names are visible where, Python Scope and the LEGB Rule walks through it, and What Does if __name__ == “__main__” Do in Python? covers the guard itself.
Become a Member to join the conversation.

jurib on July 1, 2026
Why does the script raise an error when
QUOTESis insidemain()but not when it’s outside?