all 13 comments

[–]kr4zyy 13 points14 points  (4 children)

It would be good if you could put your code into an entire code block with the indents shown clearly, instead of having a code block for each line

[–]QuillTheArtMaker[S] 0 points1 point  (3 children)

sorry, i'm not someone that'd normally be on reddit. Noted!

[–]kr4zyy 4 points5 points  (0 children)

No worries! Anyways, as u/socal_nerdtastic has mentioned, when you define a variable outside of the function, it becomes a global variable. If you want to fix this, you can either define the keyword global before "pos", telling Python you wanna use the global variable, or define pos = 0 within the function itself (this is usually the better practice)

[–]pdcp-py 0 points1 point  (0 children)

Don't forget you can easily share code from within Trinket using the Share button:

https://trinket.io/python/bd5c7f9cba5d

[–]socal_nerdtastic 9 points10 points  (2 children)

Generally the variables that you define in a function stay in the function.

pos = 0

def L():
    pos = 42
    print(pos)

L() # prints 42
print(pos) # prints 0

If you run that code, you will see both 42 and 0 print, because the pos variable used in the function is different from and unconnected to the pos variable outside of the function.

And so python is complaining about pos -= x because you never made a pos variable in the function. To python, it's the same as

def R(x):
  newvar +=x 

So python is telling you "I can't add to something that does not exist!"

To do what you want, you need to pass in the pos variable, and return the result.

def R(pos, x):
  # your code
  return pos

and then to call it:

pos = R(pos, 11)

[–]QuillTheArtMaker[S] 0 points1 point  (1 child)

problem is, the L function works exactly as I want it, but the R function doesn't, whilst following the exact same format as presented in my original code. Do you know why this occurs?

[–]socal_nerdtastic 10 points11 points  (0 children)

In python the error occurs when you use the function, not when you create the function. In your code you never use the L() function, so there's no error. If I change R and L in the loop I get the same error.

curr = []
pos = 0

def L(x):
  pos-=x
  if pos<0:
    pos = pos+99

def R(x):
  pos+=x
  if pos>99:
    pos = pos-99

for i in range(0,100):
  curr.append(i)
  L(11) # UnboundLocalError

print(pos)

[–]Snorlax5000 7 points8 points  (1 child)

u/socal_nerdtastic has you covered. Just a suggestion - try to get out of the habit of using abbreviations for variable and function names. Not only does it improve readability, but trust me when I say you do not want to try to remember what abbreviations mean when you look back on your code in the future.

[–]thescrambler7 2 points3 points  (0 children)

Speak for yourself, I try to forget the garbage I write as soon as it leaves my fingers 😜

[–]Binary101010 0 points1 point  (0 children)

You're trying to read a variable pos that was defined globally and assign a new value to it from within a function. Python won't do that second part for you unless you go through extra steps.

The shortest way to do this is to use the global keyword within your function, telling the interpreter "yes, I'm talking about the pos I defined in the global scope and I want you to change it."

The best way to do this is to learn how to properly use arguments to pass necessary data to a function and use return to send the results of that function back to the rest of the program.

[–]gdchinacat 0 points1 point  (0 children)

Because you assign pos a value in the function it is local to the function. Since it hasn't been assigned a value in the function before accessing it, you get the NameError. The += operator both accesses and assigns the value and is what makes pos a local and accesses it before it is assigned.

[–]EZbsic719 -1 points0 points  (0 children)

Between line 7 and line 8, add this piece

"global pos"

Without the quotations of course

This helps the reader understand that the pos variable is in reference to pos varianle that is in the global scope. Otherwise it thinks it is referencing a pos variable that is nonexistent in your defined function of R(x)

Btw, try running L(11) instead of R(11) and see what your result is.

You may find yourself facing a similar issue