python - Local variable referenced before assignment inside function -


i'm sorry if seems stupid. code results in error: unboundlocalerror: local variable 'current_order' referenced before assignment, in first line of second 'for' loop. error fixed if declare current_order global variable; however, still can't understand why have this. haven't created variable in first loop? (by way, conditional in first loop guaranteed return true not issue). many thanks

def choose_pitch_from_order(current_pitch, direction, pitches_in_play,  chomp_key):       pitch in all_pitches:         if current_pitch == pitch.name:             current_order = pitch.order      in current_order:         pitch in pitches_in_play:             if pitch.index == i:                 next_set = pitch                 pitches_in_play.remove(next_set)                 return (next_set, direction, chomp_key) 

it seem condition inside first loop not execute, because current_pitch not equal pitch.name @ point, current_order never assigned. if case, you'll want define current_order outside default value, code not fail.

def choose_pitch_from_order(current_pitch, direction, pitches_in_play,  chomp_key):     current_order = [] # default value, declaration outside loop/condition     ... 

remember default value must sort of iterable (list, tuple, set, or other iterable), otherwise typeerror thrown @ second loop.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -