2016-03-01 45 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之前。

相关问题