为什么出现意外缩进?

发布于 2024-08-16 23:34:08 字数 1878 浏览 9 评论 0原文

为什么会出现这种情况?

def LoadPackageList():
    try:
        #Attempts to load package list... Adds each neccessary attribute into array
        print("Loading Package List... please wait")
        packages = []
        packagelisturl = os.getcwd() + "packages.list"
        dom = minidom.parse(urllib.urlopen(packagelisturl))
        try:
            for eachattributeofpkglist in dom.GetElementsByTagNameNS(packagelist, 'packages'):
                packages.append({
                    'title': node.getAttribute('title'),
                    'shortname': node.getAttribute('shortname'),
                    'dlurl': node.getAttributes('dlurl'),
                    'description': node.getAttributes('description'),
                    'tags': node.getAttributes('tags'),
                    'infopage': node.getAttributes('infopage'),
                    'quality': node.getAttributes('quality'),
                    'id': node.getAttributes('id')
            })

        except LoadPackageListFailed:
            print("Loading Package List failed... try again soon or manually update this release!")
            Write2ErrorLog(LoadPackageListFailed)
#Indent Here Fails            
def Usage():
#prints usage and closes
    print ("Invalid Argument Specified, please retry using the format stated below\n")
    print ("*** Simtho Usage Parameters ***\n")
    print ("-i Installs Local App, include full path")
    print ("-u Uninstalls Installed App,include ID or Name")
    print ("-l Lists all installed Apps and their ID")
    print ("-a Lists All Apps in Repository")
    print ("-s Downloads and Installs App from repository, enter the title or id number")
    print ("-w Downloads and Installs Single App from a full link")
    print ("-r Removes All Packages installed\n")
    print ("*** End of Simtho Usage ***")
    os._exit(1)
    return;

Why does this happen?

def LoadPackageList():
    try:
        #Attempts to load package list... Adds each neccessary attribute into array
        print("Loading Package List... please wait")
        packages = []
        packagelisturl = os.getcwd() + "packages.list"
        dom = minidom.parse(urllib.urlopen(packagelisturl))
        try:
            for eachattributeofpkglist in dom.GetElementsByTagNameNS(packagelist, 'packages'):
                packages.append({
                    'title': node.getAttribute('title'),
                    'shortname': node.getAttribute('shortname'),
                    'dlurl': node.getAttributes('dlurl'),
                    'description': node.getAttributes('description'),
                    'tags': node.getAttributes('tags'),
                    'infopage': node.getAttributes('infopage'),
                    'quality': node.getAttributes('quality'),
                    'id': node.getAttributes('id')
            })

        except LoadPackageListFailed:
            print("Loading Package List failed... try again soon or manually update this release!")
            Write2ErrorLog(LoadPackageListFailed)
#Indent Here Fails            
def Usage():
#prints usage and closes
    print ("Invalid Argument Specified, please retry using the format stated below\n")
    print ("*** Simtho Usage Parameters ***\n")
    print ("-i Installs Local App, include full path")
    print ("-u Uninstalls Installed App,include ID or Name")
    print ("-l Lists all installed Apps and their ID")
    print ("-a Lists All Apps in Repository")
    print ("-s Downloads and Installs App from repository, enter the title or id number")
    print ("-w Downloads and Installs Single App from a full link")
    print ("-r Removes All Packages installed\n")
    print ("*** End of Simtho Usage ***")
    os._exit(1)
    return;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩物 2024-08-23 23:34:08

尝试通过 python -t 运行它,看看其中是否存在制表符和空格的混合。

附注:使用 optparse处理命令行参数。它将使您的生活变得更加轻松并产生良好的一致界面。

Try running it through python -t and see if there is a mixture of tabs and spaces somewhere in there.

As a side note: use optparse to process command line parameters. It will make your life much easier and produce a nice consistent interface.

如果没有你 2024-08-23 23:34:08

您发布的代码工作得很好。因此,请检查您发布的部分上方的行。如果这没有帮助,请发布确切的错误消息和更多代码。

编辑:还要检查是否混合使用了制表符和空格。

编辑2:(响应OP发布的更多代码)。
啊哈。每个 try 都需要一个 except。在LoadPackageList的定义中,有两个try。内部有一个 except 块,但外部只是一个空的 try

The code you posted works just fine. Therefore, check the lines immediately above the part you posted. If that does not help, please post the exact error message and a little more of the code.

Edit: Also check that you do not have a mixture of tabs and spaces.

Edit 2: (In response to more code posted by the OP).
Aha. Every try requires an except. In the definition of LoadPackageList, there are two try's. The inner one has an except block, but the outer one is just a bare try.

笑饮青盏花 2024-08-23 23:34:08

第 2 行的 try 不应该有一个 except、finally 或 else 与其关联吗?或者这是我以前从未见过的一些新的 Python 习惯用法?

Shouldn't the try on line 2 have an except, finally or else associated with it? Or is this some new Python idiom I haven't seen before?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文