如果這看起來很愚蠢,我真的很抱歉。此代碼導致錯誤:UnboundLocalError:在賦值之前引用的局部變量'current_order',特別是第二個'for'循環的第一行。 如果我將current_order聲明爲全局變量,那麼錯誤是固定的;但是,我仍然不明白爲什麼我必須這樣做。我沒有在第一個for循環中創建變量嗎? (順便說一下,第一個for循環中的條件保證返回True,所以這不是問題)。 非常感謝在函數內部賦值之前引用的局部變量
def choose_pitch_from_order(current_pitch, direction, pitches_in_play,
chomp_key):
for pitch in all_pitches:
if current_pitch == pitch.name:
current_order = pitch.order
for i in current_order:
for pitch in pitches_in_play:
if pitch.index == i:
next_set = pitch
pitches_in_play.remove(next_set)
return (next_set, direction, chomp_key)
如果'如果current_pitch == pitch.name'永遠不會計算爲'True'會發生什麼? –
@JonathonReinhart他聲稱永遠不會發生。 – Barmar
...或者'all_pitches'是空的。 –