Я присваиваю remaining_length
значение длины запаса.Длина запаса назначается в другой функции и также является глобальной.Я пытаюсь запустить код, и он говорит мне, что я использую переменную перед присваиванием.Я объявил и использовал другие глобальные переменные в своем коде, и до сих пор не имел этой проблемы.Кроме того, почему он распознает мою глобальную переменную all_possible_cutting_options
, но не remaining_length
?Я переместил remaining_length
в get_next_possible_cutting_option()
, и это работает, но мне нужно сохранить значение для remaining_length
и использовать его снова при следующем вызове get_next_possible_cutting_option()
вместо того, чтобы каждый раз возвращать значение обратно в stock_length
.
def get_all_possible_cutting_options_for_a_bar():
global all_possible_cutting_options
global remaining_length
remaining_length = stock_length
all_possible_cutting_options = []
another_cutting_option_possible = get_another_cutting_option_possible()
while another_cutting_option_possible:
get_next_possible_cutting_option()
another_cutting_option_possible = get_another_cutting_option_possible()
def get_next_possible_cutting_option():
cutting_option = []
for cut in cuts_ordered:
if remaining_length >= cut.length:
cut.quantity = remaining_length // cut.length
remaining_length -= cut.length * cut.quantity
cutting_option.append(cut)
else:
cut.quantity = 0
cutting_option.append(cut)
all_possible_cutting_options.append(cutting_option)
Ошибка:
Traceback (most recent call last):
File "main-v3.0.py", line 91, in <module>
get_all_possible_cutting_options_for_a_bar()
File "main-v3.0.py", line 41, in get_all_possible_cutting_options_for_a_bar
get_next_possible_cutting_option()
File "main-v3.0.py", line 56, in get_next_possible_cutting_option
if remaining_length >= cut.length:
UnboundLocalError: local variable 'remaining_length' referenced before assignment