所以我一直在研究一个程序,它可以读取一个文件,里面有多个名字,我只需要做一个程序,这样它就可以对文件名进行排序了。在
像这样:
像上面这样的一个列表,但是已经分类了!PrList.sort()
是我需要帮助的地方,它在main函数中。在
def readfile(filename):
infile = open(filename, "r")
filetext = infile.read().splitlines()
infile.close()
return filetext
def printlist(list):
for i, item in enumerate(list):
print(i, ': ', item, sep="")
def linear_search(search, value):
for i in range(len(search)):
if search[i] == value:
return i
def main():
filename = input('File name: ')
print()
read = readfile(file)
PrList = printlist(read)
print()
PrList.sort()
while True:
name = input('Enter the name a name: ')
if name == 'quit':
sys.exit()
else:
ls = linear_search(read, name)
print('The position of', name, 'is: \nLinear search: ', ls)
print()
main()
错误消息:
^{pr2}$
PrList=打印列表(读取)
函数printlist()返回None作为默认值,您只需打印出列表,而应该返回它。在
相关问题 更多 >
编程相关推荐