Question

I'm attempting to get the cost (the values at the end of each line) total from all the logs but I keep getting the error " slice indices must be integers or None or have an index method ." Each line has varying length before the value. This code does not yet add the cost but I'm making sure I'm getting.

log record entered into function (rec.dat): *this log would be the record entered into the function

@ 2014 2 14 00:03:01 Matt "login" 0.01

@ 2014 2 14 02:06:12 Mary "login" 0.01

@ 2014 2 14 17:12:05 Mary "cd ~/cs150/projects" 0.01

function:

the goal of the function is to add how much the cost would be for all actions (in this case cost should end up totaling .03)

def cost(rec):
    s = Scanner(rec)
    cost = 0
    line = s.readline()
    for i in range(0, len(rec), 1):
        info = len(line) - 3
        n = float(info)
        price = line[n:0]
        cost += price
        line = s.readline()
    s.close()
    return cost
Was it helpful?

Solution

You cannot use a float for slicing, replace:

n = float(info)
price = line[n:0]

with:

price = line[info:0]

You don't need n variable at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top