2016-03-01 67 views
-3

我試圖讓那一個用戶輸入相匹配的應用程序,並顯示百分比它是屬於(預定義的句子)指數錯誤:列表分配索引超出範圍

store_word="" 
def client_query_function(): 
    point =0 
    i=0 
    z=0 #tells the index number of the sentence 
    p="" 
    global store_word 
    client_query = raw_input(">> ") 
    len_client_query=len(client_query) 
    i=len_client_query 
    for z in range(i): 
     #print client_query[z] 
     if client_query[z]!=" ": 
       p=p+client_query[z]  
       store_word="" 
     else: 
      store_word=p 
      print "store_word" ,store_word 

      #dictonary search 

timeis =[] 
dateis = [] 

client_query_function() 

def timeis_funct(store_word): 
    global pm_timeis  
    timeis[5]=["what","is","the","time","?",""] 
    for i in range (len(timeis)): 
     if store_word==timeis[i]: 
      pm_timeis=pm_timeis+1 
     else: 
      continue 
    return pm_timeis 

def dateis_funct(store_word): 
    global pm_dateis  
    dateis[5]=["what","is","the","date","?",""] 
    for i in range (len(dateis)): 
     if store_word==dateis[i]: 
      pm_dateis=pm_dateis+1 
     else: 
      continue 
    return pm_dateis 


def percent_cal(): 
    timeis_funct(store_word) 
    dateis_funct(store_word) 
percent_cal() 

這句話在運行程序的顯示列表分配索引超出範圍,這不應該發生

+2

追溯請嗎? –

+0

提供有關發生的錯誤的更多信息。它發生在哪一行? –

+0

我假設這個錯誤發生在「timeis [5] = [」what「,」is「,」the「,」time「,」?「,」「]」行,對不對? – rayray84

回答

4
timeis[5]=["what","is","the","time","?",""] 

這並不是創建一個名爲timeis與五行列表的正確方法。在左邊的索引是不必要的。試試:

timeis=["what","is","the","time","?",""] 

dateis[5]=["what","is","the","date","?",""]一樣。


此外,你會得到NameError: global name 'pm_timeis' is not defined,因爲你從來沒有用這個名字,你使用它的函數內部之前聲明一個變量。如果變量不存在,global語句不會創建該變量;你仍然需要自己做。你將需要添加

pm_timeis=0 
pm_dateis=0 

某處在你的代碼中調用timeis_functdateis_funct之前。

相關問題