Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. I try to add def checklen(max_list_from_all_plates, listaMain, ListaVal):
Qt 6.11 is out! See what's new in the release blog

I try to add def checklen(max_list_from_all_plates, listaMain, ListaVal):

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 831 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coutKateM
    wrote on last edited by coutKateM
    #1

    My error in code :
    local variable 'maxelement' referenced before assignment

    class 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!

    JonBJ 1 Reply Last reply
    0
    • C coutKateM

      My error in code :
      local variable 'maxelement' referenced before assignment

      class 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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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"....

      1 Reply Last reply
      0
      • C Offline
        C Offline
        coutKateM
        wrote on last edited by coutKateM
        #3

        I fixed like you was recomending

        JonBJ 1 Reply Last reply
        0
        • C coutKateM

          I fixed like you was recomending

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @coutKateM
          So if your code never enters the for mainresult in plateresults it never sets maxelement. Then it tries to print(maxelement), that is undefined, hence the error.

          You need to initialize it before the for loop, 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 used self anywhere. That is surprising, but again up to you.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved