2009-12-31 58 views
1

這是爲什麼發生?爲什麼意外縮進?

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; 
+2

縮進的位置究竟是什麼?你能展示實際的產出和你的期望嗎? – Rory

+1

嘗試通過顯示空白的東西運行它。在* nix機器上,使用'od -c yourfile.py'來查看字符。這可能是有用的,雖然可能有更好的方法來做到這一點。 – Ibrahim

+0

如果您還沒有弄清楚,請複製上面的代碼並粘貼到您自己的代碼上面,它應該可以解決問題。如果沒有,問題不在上面的代碼中,而是在相鄰的地方,正如〜unutbu建議的那樣。 –

回答

3

不應該在第2行的嘗試有一個除了,最後還是與之相關聯?或者這是我以前從未見過的一些新的Python成語嗎?

+0

是的,這樣做,因爲我把一個不需要的嘗試聲明或W/E類的東西嘗試是 –

+1

嘿cuzzin,你沒有沒有代碼是葡萄乾'LoadPackageListFailed' –

4

您發佈的代碼工作得很好。因此,請檢查您發佈的部分上方的行。如果這沒有幫助,請發佈確切的錯誤消息和更多的代碼。

編輯:同時檢查你沒有標籤和空格的混合。

編輯2:(響應OP發佈的更多代碼)。 啊哈。每個try需要except。在LoadPackageList的定義中,有兩個try's。內部有一個except塊,但外部塊只是一個裸露的try

7

嘗試運行它通過python -t並查看是否有標籤和空間的混合物在那裏的某處。

附註:使用optparse來處理命令行參數。它會讓你的生活更輕鬆,併產生一個很好的一致的界面。