2017-07-27 76 views
1

我似乎無法弄清楚爲什麼這個Python代碼中的「else:」存在解析錯誤。請幫忙。 我正在嘗試使用來自文本「mbox-short.txt」的字典來計算出現相同「電子郵件」的次數。最終的結果應該是打印密鑰(即電子郵件)和值(重複次數)。解析錯誤 - 其他

name = raw_input("Enter file:") 
if len(name) < 1 : name = "mbox-short.txt" 
try: 
    handle = open(name) 
except: 
    print 'Invalid input.' 
    quit() 

def emailrepeat(s): 

    d = dict() 

    if s not in d: 
     d[s] = 1 
    else: 
     try: 
      d[s] = d[s] + 1 
     except: 
      print "error" 

    return d 


for line in handle: 
    if line.startswith('From:') == True: 
     continue 

    if line.startswith('From') == True: 
     newline = line.split() 
     email = newline[1] 
     print email 

     print emailrepeat(email) 

謝謝大家!

+3

是否在你的代碼的其他部分中的星號?如果不是,我會刪除它們並用註釋表明問題位置。 –

回答

0

**else**是什麼意思?

enter image description here

如果您想注意else語句,請使用#'''

enter image description here

+1

這應該是一條評論。 –

0

刪除星號其他各地,使其他註釋使用#「」」或‘’」 星號將導致錯誤