I try to add def checklen(max_list_from_all_plates, listaMain, ListaVal):
-
My error in code :
local variable 'maxelement' referenced before assignmentclass PlateString: #def __init__(seq, threshold=0.05): max_list_from_all_plates = [] threshold = 0.05 for single_lp in seq: maximum_plate_prob = [] possible_plate_prob = [] max_list = [] lista = [] newlist = [] for sign in single_lp: high_indexes = [] for prob_id in range(0,len(sign)): if threshold<sign[prob_id]: high_indexes.append([sign[prob_id], prob_id, CATEGORIES[prob_id]]) max_list.append(high_indexes) max_list_from_all_plates.append(max_list) listaMain = [] ListaVal = [] plateresults = [] def checklen(max_list_from_all_plates, listaMain, ListaVal): for plate in max_list_from_all_plates: maxlen = 0 if len(plate) > maxlen: maxlen = len(plate) textPlate : str= "" sumplate = 0 for probabilities in plate: znakMax : str = "" probabilityMax : float = 0 for probability in probabilities: if(probabilityMax<probability[0]): probabilityMax = probability[0] znakMax = probability[2] valMax = probability[0] textPlate += znakMax sumplate += valMax listaMain.append(textPlate) ListaVal.append(sumplate) print(listaMain, ListaVal) def findtotal(plateresults, listaMain, ListaVal): for i in range(len(listaMain)): info = [ListaVal[i], listaMain[i]] plateresults.append(info) print(plateresults) for mainresult in plateresults: maxresult = max(mainresult[0] for mainresult in plateresults) maxelement = max((x) for x in plateresults) print(maxelement)# ERROR:local variable 'maxelement' referenced before assignment if __name__ == '__main__': findtotal(plateresults, listaMain, ListaVal)Thank you in advance!
-
My error in code :
local variable 'maxelement' referenced before assignmentclass PlateString: #def __init__(seq, threshold=0.05): max_list_from_all_plates = [] threshold = 0.05 for single_lp in seq: maximum_plate_prob = [] possible_plate_prob = [] max_list = [] lista = [] newlist = [] for sign in single_lp: high_indexes = [] for prob_id in range(0,len(sign)): if threshold<sign[prob_id]: high_indexes.append([sign[prob_id], prob_id, CATEGORIES[prob_id]]) max_list.append(high_indexes) max_list_from_all_plates.append(max_list) listaMain = [] ListaVal = [] plateresults = [] def checklen(max_list_from_all_plates, listaMain, ListaVal): for plate in max_list_from_all_plates: maxlen = 0 if len(plate) > maxlen: maxlen = len(plate) textPlate : str= "" sumplate = 0 for probabilities in plate: znakMax : str = "" probabilityMax : float = 0 for probability in probabilities: if(probabilityMax<probability[0]): probabilityMax = probability[0] znakMax = probability[2] valMax = probability[0] textPlate += znakMax sumplate += valMax listaMain.append(textPlate) ListaVal.append(sumplate) print(listaMain, ListaVal) def findtotal(plateresults, listaMain, ListaVal): for i in range(len(listaMain)): info = [ListaVal[i], listaMain[i]] plateresults.append(info) print(plateresults) for mainresult in plateresults: maxresult = max(mainresult[0] for mainresult in plateresults) maxelement = max((x) for x in plateresults) print(maxelement)# ERROR:local variable 'maxelement' referenced before assignment if __name__ == '__main__': findtotal(plateresults, listaMain, ListaVal)Thank you in advance!
@coutKateM
Please use the forum post's Code tags around your code to make your code legible.You should state what your issue is, what error you get on what line, etc. not just "but have some errors"....
-
@coutKateM
So if your code never enters thefor mainresult in plateresultsit never setsmaxelement. Then it tries toprint(maxelement), that is undefined, hence the error.You need to initialize it before the
forloop, in case that never gets entered.This has nothing to do with your
__init__(). With it commented out, as you show, all your variables are Python class variables. I doubt that is what you intend, but that's up to you. You don't seem to have usedselfanywhere. That is surprising, but again up to you.