繁体   English   中英

IndentationError:在第 51 行的 function 定义之后需要一个缩进块

[英]IndentationError: expected an indented block after function definition on line 51

`

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):
    

` 代码输出以下错误 IndentationError: expected an indented block after function definition on line 51 我不确定为什么会这样,因为第 51 行没有代码。我是 python 的新手,所以不完全熟悉

最初我对该行有评论,所以我将其删除,但这没有任何好处。

你在最后做了一个空的 function 没有任何东西吗?

删除它或这样做:

def optimize(self, event):
    pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM