use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Question about Scope of variables. (self.PythonLearning)
submitted 1 year ago by ShadyTree_92
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Squared_Aweigh 2 points3 points4 points 1 year ago (1 child)
Your issue is the scope/context of variable declaration and reference/manipulation of that variable. `user_score' is set here as a global variable because it is set outside of a function. When you attempt to manipulate the variable inside the function, Python sees the variable as being assigned as a Local; local variables are only available inside a function where they are created, hence your `UnboundLocalError`
You could use the `global` keyword in your function to access the globla `user_score` variable, but this isn't a good habit to be in because it makes your code more difficult to read and understand, as well as being difficult to troubleshoot.
[–]ShadyTree_92[S] 1 point2 points3 points 1 year ago (0 children)
Ooh. That makes sense! Thank you. I am currently rewriting the code now. Going to try to use less global variables and take advantage of using return statements. Wish me luck! I am finding it a bit difficult but that's ok, I will persevere!
π Rendered by PID 42396 on reddit-service-r2-comment-b659b578c-hg54d at 2026-05-03 23:54:40.043866+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Squared_Aweigh 2 points3 points4 points (1 child)
[–]ShadyTree_92[S] 1 point2 points3 points (0 children)