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

时间:2019-03-13 16:58:08

标签: python python-3.x

好的,因此,最糟糕的是,我的程序目前必须从已经正常工作的CSV文本文件中获取数据,但是当我尝试将max设置为distance [counter]时,出现错误我了解的状态(在'str'和'float'的实例之间不支持TypeError:'>'),但我一直在努力使它发挥作用,对此表示感谢

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)

1 个答案:

答案 0 :(得分:1)

line是一个字符串,因此拆分时会得到一个字符串列表。然后,您尝试将这些字符串与浮点数进行比较,而浮点数会失败。

如果您可以保证距离始终是一个数字,则可以简单地将distance[counter] = items[2]替换为distance[counter] = float(items[2])