枚举期间此代码有什么问题

时间:2016-04-18 18:31:46

标签: python python-2.7 python-3.x

import datetime
with open("fine.txt","r") as f, open("fine1.txt","a") as fine1:
    lines = f.read().split("\n")
    for i in range(2):
        var = input("reg : ") # registration number(reg_num)
        enter = input('Time entered camera 1(24hrs)in the format HH:MM:SS:   ')
        ext = input('Time enterd camera 2 (24hrs)in the format HH:MM:SS : ')
        total_time = '%H:%M:%S'
        enter_time = datetime.datetime.strptime(enter, total_time)
        ext_time = datetime.datetime.strptime(ext, total_time)
        if enter_time > ext_time:
            ext_time += datetime.timedelta(hours=24)
        t_diff = ext_time - enter_time
        time = t_diff.total_seconds() / 3600
        speed = 1 / time
        reg = var[0:1].isalpha() and var[2:3].isdigit() and var[4].isspace() and var[5:7].isalpha() and var.isupper()
        if reg == True:
            for i, line in enumerate(lines):
                if var in line:
                    num = int("{}".format(i))
                    var = f.read()
                    name = (var[num])   #the problem
                    print(name)
                    address = (var[num + 0])
                    if speed > 70:
                        print("ovrspeeding", (var[num + 0]))
                        fine1.write(name+address+speed+"\n")

整个代码必须输入,否则你将无法理解我想要做什么。

fine.txt 是一个已经制作的文件,如下所示:

reg_num1 aaaaaaaaaaaaaaaaaaa
reg_num2 bbbbbbbbbbbbbbbbbbb
reg_num3 ccccccccccccccccccc

此代码接收注册号的输入(例如AA01 SSS)和2种时间格式(稍后将用于计算速度)。我希望这段代码能够找到输入了注册号i的 fine.txt 中的行,如果该车辆超速(速度> 70mph),整行需要附加到文件中 fine1.txt

问题是,当我运行代码时,错误按摩说明:

name = (var[num])
IndexError: string index out of range

我不知道这意味着什么,所以你可以帮我解决这个问题。

0 个答案:

没有答案