Python Forum
why am I getting "local variable 'x' referenced before assignment"?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why am I getting "local variable 'x' referenced before assignment"?
#4
he meant for not if
however you don't want it to be global
just move the x = 0 line in the function.
Then you will face a new problem, because your function does not return anything, so it will return None and that's what you will get printed.... You need to fix this as well.

Note that this is not the best approach to sum elements of a list. There is built-in function that does the same
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: why am I getting "local variable 'x' referenced before assignment"? - by buran - Jun-16-2018, 05:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rounding exercise: UnboundLocalError: local variable referenced before assignment Drone4four 5 3,683 Sep-06-2020, 09:01 AM
Last Post: ibreeden
  UnboundLocalError: local variable 'a' referenced before assignment fad3r 3 16,715 Jun-20-2018, 05:43 PM
Last Post: nilamo
  variable referenced before assignment Niko047 4 23,194 Aug-04-2017, 07:55 PM
Last Post: nilamo
  local variable 'l' referenced before assignment... darkreaper1959 4 7,643 Jan-21-2017, 08:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020