我是新來的python我發現這個問題的一些答案,但沒有什麼能真正幫助我。在分配之前引用的局部變量
這裏的代碼部分與我有問題:
batch_index=0 # initializing a globale variable, I've tried with global batch_index too
..................................
def next_Training_Batch():
if batch_index < len(train_data):
batch_index = batch_index + 1
return train_data[batch_index-1], train_labels[batch_index-1]
else :
shuffle_data()
batch_index = 0
return train_data[batch_index], train_labels[batch_index]
當我打電話,我得到了以下的功能:
UnboundLocalError: local variable 'batch_index' referenced before assignment
我不想使用參數該函數(如同類問題中所建議的),並且說實話,我使用了「全局」變量的數目而沒有任何錯誤,我不明白爲什麼我不允許在if語句中對它進行評估? 感謝您的任何提示!
@chris_Rands我想我這樣做了,其他代碼不用batch_index也沒有相關的功能 – Engine
BTW什麼,那就是避免使用修改的全局變量是一個好主意:他們使代碼更少模塊化,因此更難進行調試和修改。 –
@ PM2Ring感謝提及它! – Engine