乘以csv值 - TypeError:不能将序列乘以'float'类型的非int

时间:2017-11-30 01:42:57

标签: python arrays python-3.x list csv

因此,我为单身工作编写的部分代码要求我将csv列表中的排名乘以锦标赛的难度。

我已经尝试了所有我知道的但我不断得到 TypeError:不能将序列乘以'float'类型的非int

这就是我现在所拥有的:

import csv
class Opener:

    with open("DADSA 17-18 COURSEWORK A RANKING POINTS.csv", "r") as csv_file3:
        next(csv_file3)
        rp = csv.reader(csv_file3)
        ranking_points = []
        for rowrp in rp:
            if len(rowrp) != 0:
                ranking_points += [rowrp]

List with the ranking points(left)

下载csv文件here

tourn=input("What is this tournament: ").upper()

def tournament(tourn):
    rp0 = Opener.ranking_points[0][0]
    multiply = 0
    for line in rp0:
        if tourn == "TAC1":
            multiply = 2.7
            rp0 *= multiply
        elif tourn == "TAE21":
            multiply = 2.3
            rp0 *= multiply
        elif tourn == "TAW11":
            multiply = 3.1
            rp0 *= multiply
        elif tourn == "TBS2":
            multiply = 3.25
            rp0 *= multiply
    return rp0
tournament(tourn)

TAC1 TAE21 TAW11 TBS2 ,是难度等级的名称。

我希望你们中的一个可以帮助我,我已经尝试了一些我在这里找到的东西,但最后都归结为同样的错误。

编辑: 我最初提出的错误来自之前的尝试,其中我尝试了别的东西。

此代码的完整错误如下所示:

追踪(最近一次通话):   文件“D:/Transferencias/DADSA/Tournament.py”,第62行,

tournament(tourn)

文件“D:/Transferencias/DADSA/Tournament.py”,第50行,锦标赛

rp0 *= multiply

TypeError:不能将序列乘以'float'

类型的非int

1 个答案:

答案 0 :(得分:0)

特别注意这一行

ranking_points += [[float(i) for i in rowrp]]

你必须创建一个浮动列表,否则你将无法与其他浮点数相乘

import csv
class Opener:

    with open("DADSA 17-18 COURSEWORK A RANKING POINTS.csv", "r") as csv_file3:
        next(csv_file3)
        rp = csv.reader(csv_file3)
        ranking_points = []
        for rowrp in rp:
            if len(rowrp) != 0:
                ranking_points += [[float(i) for i in rowrp]]


tourn=input("What is this tournament: ").upper()

def tournament(tourn):
    # global Opener.ranking_points[0][0]
    rp0 = Opener.ranking_points
    # print(rp0)
    multiply = 0
    for line in rp0:
        print(line[0])
        if tourn == "TAC1":
            multiply = 2.7
            line[0] *= multiply
        elif tourn == "TAE21":
            multiply = 2.3
            line[0] *= multiply
        elif tourn == "TAW11":
            multiply = 3.1
            line[0] *= multiply
        elif tourn == "TBS2":
            multiply = 3.25
            line[0] *= multiply
    return rp0

print(tournament(tourn))