为什么出现意外缩进?

1 投票
3 回答
4662 浏览
提问于 2025-04-15 17:30

这为什么会发生呢?

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;

3 个回答

4

你发的代码运行得很好。所以,先检查一下你发的代码上面几行。如果这样还不行,请把具体的错误信息和更多的代码发上来。

编辑: 还要检查一下,确保没有混用制表符和空格。

编辑 2:(针对提问者发的更多代码)哦,每个 try 都需要有一个 except。在 LoadPackageList 的定义里,有两个 try。里面的那个有 except 块,但外面的那个只是一个单独的 try

7

试着用 python -t 来运行一下,看看代码里是否混用了制表符和空格。

顺便提一下:可以使用 optparse 来处理命令行参数。这样会让你的工作简单很多,而且能提供一个一致的界面。

3

第二行的try后面不应该有except、finally或者else吗?还是说这是我没见过的新Python写法?

撰写回答