Make that:
price = line[-3:]
You can delete info
completely. Look up how Python's slice notation works, it provides handy shortcuts for a large number of problems, including yours :-)
문제
I'm attempting to get the cost (the values at the end of each line) total from all the logs but when price is printed it doesn't print anything just a blank space. 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
price = line[info:0]
print("price: ",price)
cost += price
line = s.readline()
s.close()
return cost
해결책
Make that:
price = line[-3:]
You can delete info
completely. Look up how Python's slice notation works, it provides handy shortcuts for a large number of problems, including yours :-)
다른 팁
It seems to me 'price = line[info:0] needs to be 'price = line[info:]'
>>> a = 'abcde'
>>> a[2:]
'cde'
>>> a[2:0]
''
>>>