The error you're encountering:
UnboundLocalError: cannot access local variable 'redraw_args' where it is not associated with a value
happens because redraw_args is only assigned inside the try block, but you are trying to use it in the except block. If the int(len_str) conversion fails, the code jumps to the except block before redraw_args is assigned, so it doesn't exist yet.
How to fix it
You need to define redraw_args before the try block, so it is always available, even if an exception occurs.
Example fix:
def _validate_and_start():
redraw_args = (gs, config_module, len_str, guess_str, active_field) # define before try
try:
diff = int(len_str)
g_count = int(guess_str)
if not (3 <= diff <= 10):
show_message_box(gs, "Длина слова должна быть 3-10!", 3000, _draw_custom_difficulty_inputs, redraw_args)
return False
if not (1 <= g_count <= 15):
show_message_box(gs, "Попыток должно быть 1-15!", 3000, _draw_custom_difficulty_inputs, redraw_args)
return False
if not (diff in gs.WORD_SETS and gs.WORD_SETS[diff]):
show_message_box(gs, "Нет слов такой длины!", 3000, _draw_custom_difficulty_inputs, redraw_args)
return False
gs.difficulty = diff
gs.custom_guesses_count = g_count
return True
except ValueError:
show_message_box(gs, "Ошибка ввода чисел!", 1000, _draw_custom_difficulty_inputs, redraw_args)
return False
Why?
redraw_args is needed in both the try and except blocks.
- If
int(len_str) fails, the code jumps to except without ever assigning redraw_args.
- Defining it before the
try ensures it always exists.
If len_str, guess_str, or other variables used in redraw_args might not be defined before this function, make sure they are initialized or passed properly.
If you want me to review the full function or surrounding code for better context, feel free to share!