代码之家  ›  专栏  ›  技术社区  ›  xnx

努力学习更高的课程。尝试设置新的最大值时出错

  •  0
  • xnx  · 技术社区  · 5 年前

    类型错误:'>'“str”和“float”实例之间不支持

    forename = [""] * 100
    surname = [""] * 100
    distance = [0.0] * 100
    
    
    # Gets the members details from the file
    
    #strip the file by /n to get seperate lines
    
    # split it by commas to get each value in each line
    
    def get_members_info():
        counter = 0
        with open("members.txt",'r') as readfile:
            line = readfile.readline().rstrip('/n')
            while line:
                items = line.split(",")
                forename[counter] = items[0]
                surname[counter] = items[1]
                distance[counter] = items[2]
    
                line = readfile.readline().rstrip('/n')
                counter +=1
        return forename, surname, distance
    #
    def print_max_distance(forename, surname, distance):
        maximum = 0.0
        print (distance[0])
        for counter in range (1, len(distance)):
            if distance[counter] > maximum:
                maxs = distance[counter]
                print (maxs)
        print (maxs)
    
    #
    get_members_info()
    print_max_distance(forename, surname, distance)
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   David    5 年前

    line 是一个字符串,所以当你拆分它时,你会得到一个字符串列表。然后尝试将这些字符串中的一个与失败的float进行比较。

    如果你能保证距离永远是一个数字,你可以简单地替换 distance[counter] = items[2] 具有 distance[counter] = float(items[2])