I'm new to Python and a little confused with the whole nonlocal thing. Here's the problem snippet of code:
positon = 0b0
while True:
pos_choice = input("\tPlease enter your choice: ").lower()
if pos_choice == '1':
position = position | Baseball.pitcher
break
elif pos_choice == '2':
position = position | Baseball.catcher
break
elif (pos_choice == 'd') and (position != 0b0):
break
elif (pos_choice == 'd') and (position == 0b0):
print("\tChoose a position.")
else:
print("Invalid choice.")
print(position)
So this throws me :
Traceback (most recent call last):
File "driver.py", line 252, in <module>
load_student()
File "driver.py", line 142, in load_student
position = position | Baseball.catcher
UnboundLocalError: local variable 'position' referenced before assignment
Based upon what I've read on answers to other questions, the problem would be because the problematic "position" is nested two loops in from the original call (is that right?).
My main problem is that I can't figure out how to bind the two "position"s using nonlocal, though I have tried various solutions to no avail. Also, is using nonlocal a taboo like it is when using global? Thanks for the help!