sort()不起作用?继续找不到b型

2024-04-26 03:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在研究一个程序,它可以读取一个文件,里面有多个名字,我只需要做一个程序,这样它就可以对文件名进行排序了。在

像这样:

  1. 鲍勃
  2. 比尔
  3. 蒂姆
  4. 汤姆
  5. 富有

像上面这样的一个列表,但是已经分类了!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}$

Tags: name程序readsearchreturnmaindeffilename