def find ():
states = open("districts.txt", "r")
for read_info in states:
split_info = read_info.split(",")
dist_name = split_info[1]
demo_vote = split_info[2]
repub_vote = split_info[3]
#print(dist_name,demo_vote,repub_vote)
state_name = input("Which state do you want to look up? ")
no_results = (state_name + " not found")
for whole in states:
if state_name in whole:
return state_name
else:
return no_results
答案 0 :(得分:1)
您有两个主要问题:
list
并多次迭代list
,或者seek
文件返回起点以允许第二轮迭代)。要么是这样,要么删除第一个循环,它似乎解析整个文件,但不对它做任何事情(没有任何东西存储,没有使用任何东西)。state_name
出现在第一行,你只会得到一个命中;删除else
条件,并将return no_results
行移到循环之外(因此只有在循环运行完成而没有找到匹配时才执行它)