How to fix 'refrenced before assignment' in Python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to fix 'refrenced before assignment' in Python ?

I do know exactly know how can I fix this error ... but this is only a part of my code , please show me my mistake : check = True def Apply(string , i ,j): ... cmp_2 = res if cmp_1 == cmp_2: check = False if check == True: ... And in the last line , the error raises ...tell me to send my complete code , if you need it ...

31st May 2021, 5:45 PM
Ali_combination
Ali_combination - avatar
9 Answers
+ 4
Just as Lisa pointed out, check is not declared if cmp_1 != cmp_2 The solution is to declare it before testing the condition check = True if (cmp_1 == cmp_2): check = False Alternatively you can write it in a single line like this check = cmp_1 != cmp_2
31st May 2021, 9:27 PM
Ore
Ore - avatar
+ 2
Ali_combination check only exists if cmp_1 == cmp_2 (line 55), so if this statement is not true, it cannot be referenced in the following the Apple function cannot refer to the check var of line 46
31st May 2021, 7:47 PM
Lisa
Lisa - avatar
+ 2
Ore Lisa Thanks a lot for your great help 😊👍
1st Jun 2021, 5:31 AM
Ali_combination
Ali_combination - avatar
+ 1
Is there a variable named cmp_1 because it is not declared in the code snippet you shared
31st May 2021, 5:50 PM
Ore
Ore - avatar
+ 1
I think you have to share the complete function body
31st May 2021, 5:53 PM
Ore
Ore - avatar
0
Ore yep..its already declared .
31st May 2021, 5:52 PM
Ali_combination
Ali_combination - avatar
31st May 2021, 6:01 PM
Ali_combination
Ali_combination - avatar
0
Ore sorry sir ... didn't you figure the bug out ? Im still stuck with it -_-
31st May 2021, 7:44 PM
Ali_combination
Ali_combination - avatar
0
Lisa I really didnt get it ... what should I change ? The if statement should be included ... otherwise the progarm won't work ..
31st May 2021, 7:58 PM
Ali_combination
Ali_combination - avatar