我正在尝试创建一个逐个匹配用户输入的应用程序,并显示它所属的句子的百分比(预定义的句子)
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()
在运行程序时,它显示列表分配索引超出范围,这不应该发生
答案 0 :(得分: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_funct
在致电dateis_funct
和public class Eshop implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public Eshop() {}
public Eshop(int eshopId, String code, String name, String lastModified) {
this.eshopId = eshopId;
this.code = code;
this.name = name;
this.lastModified = lastModified;
}
public int eshopId;
public String code;
public String name;
public String lastModified;
public int getEshopId() {
return eshopId;
}
public void setEshopId(int eshopId) {
eshopId = eshopId;
}
public String getCode() {
return code;
}
public void setCode(String code) {
code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
name = name;
}
public String getLastModified() {
return lastModified;
}
public void setLastModified(String lastModified) {
lastModified = lastModified;
}
之前,代码中的某处。