Home > other >  IndentationError: expected an indented block after function definition on line 51
IndentationError: expected an indented block after function definition on line 51

Time:12-31

`

import wx

def optimize_groups(items, weight_limit):
    # sort the items in non-ascending order based on weight
    sorted_items = sorted(items, key=lambda x: x[1], reverse=True)

    # initialize the list of groups with an empty group
    groups = [[]]

    # iterate through the sorted items
    for item in sorted_items:
        # try adding the item to each group
        for group in groups:
            # if the item fits within the weight limit of the group, add it to the group
            if sum(x[1] for x in group)   item[1] <= weight_limit:
                group.append(item)
                break
        # if the item does not fit within any of the existing groups, create a new group for it
        else:
            groups.append([item])

    return groups

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        # create the widgets
        self.items_label = wx.StaticText(self, label="Enter the items and their weights, separated by a comma:")
        self.items_entry = wx.TextCtrl(self)
        self.weight_limit_label = wx.StaticText(self, label="Enter the weight limit for each group:")
        self.weight_limit_entry = wx.TextCtrl(self)
        self.optimize_button = wx.Button(self, label="Optimize")
        self.result_label = wx.StaticText(self, label="")

        # bind the optimize button to the optimize function
        self.optimize_button.Bind(wx.EVT_BUTTON, self.optimize)

        # create a sizer to manage the layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.items_label, 0, wx.ALL, 5)
        sizer.Add(self.items_entry, 0, wx.EXPAND|wx.ALL, 5)
        sizer.Add(self.weight_limit_label, 0, wx.ALL, 5)
        sizer.Add(self.weight_limit_entry, 0, wx.EXPAND|wx.ALL, 5)
        sizer.Add(self.optimize_button, 0, wx.ALL, 5)
        sizer.Add(self.result_label, 0, wx.ALL, 5)

        # set the sizer as the main sizer for the frame
        self.SetSizer(sizer)

    def optimize(self, event):
    

` the code is outputting the following error IndentationError: expected an indented block after function definition on line 51 I am not sure why this is happening as there is not code on line 51. I am new to python so not entirely familiar

Originally I had a comment on that line so I deleted it but that was no benefit.

CodePudding user response:

You've made an empty function at the end without anything in it?

Remove it or do this:

def optimize(self, event):
    pass
  • Related