[Community edit to give reproducible example:]
def main():
e = None
print(locals())
while not e:
try:
raise Exception
except Exception as e:
pass
main()
produces
~/coding$ python3.3 quiz2.py
{'e': None}
Traceback (most recent call last):
File "quiz2.py", line 11, in <module>
main()
File "quiz2.py", line 5, in main
while not e:
UnboundLocalError: local variable 'e' referenced before assignment
[EDITED] to include a reproducible code
I am trying to run a while-loop, and the condition I use is that the loop continues when the variable e==None
. The relevant code is below:
print("\nThe current score list contains the following people's scores: ")
score_list = open("score_list.dat", "rb")
score_name = []
e = None
while not e:
try:
score = pickle.load(score_list)
name = pickle.load(score_list)
score_name.append([score, name])
except EOFError as e:
pass
score_list_sorted=sorted(score_list)
sort_list.close()
for item in score_list_sorted:
print("Score: ", item[0], "\t", item[1])
the complete code is here: https://www.dropbox.com/s/llj5xwexzfsoppv/stats_quiz_feb24_2013.py
The data file it requires (for the quiz to run) is in this link: https://www.dropbox.com/s/70pbcb80kss2k9e/stats_quiz.dat
main()
needs to be edited to use the proper data file address:
The complete error message I received is below. This is weird because I initialized e
right before the while-loop. I hope someone can help me resolve this problem. Thanks!
Traceback (most recent call last):
File "<pyshell#217>", line 1, in <module>
main()
File "/Users/Dropbox/folder/stats_quiz_feb24_2013.py", line 83, in main
while not e:
UnboundLocalError: local variable 'e' referenced before assignment