如何纠正这个str错误?

时间:2015-07-08 18:00:16

标签: python string file types

我一直在尝试创建一个程序来记录车辆的速度并接收车牌号码。我试图创建一个文件,但我有一个错误:'str不可调用',我已经尝试了所有我知道但我似乎无法让它工作。如果你能解决这个问题,我将非常感激:)

import time
import random
import re #inputs a module for the regular expression
List_of_vehicles=[]#list for the number plates
List_of_avspeed=[]#list for the average speeds

d = float("80.00")#the speed limit


Begin = input("Press 'Enter' to start recording the time, from sensor1-> ")
Sensor1=time.time()
End = input("Press 'Enter' to stop recording the time, from sensor2-> ")
Sensor2=time.time()
Time=(Sensor2-Sensor1)
rounded_up=(round(Time,2))
average_speed = (2/(rounded_up*0.0002))
rounded_up2=(round(average_speed))
c = "mph"
print ("Your average speed in miles per hour is " +str(rounded_up2) +str(c))

v_n_p = input("Enter the vehicle number plate: ")
if re.match("[A-Z]{2}[0-9]{2}[A-Z]{3}", v_n_p):
    print("Your vehicle number plate is valid")
    List_of_vehicles.append(v_n_p)
else:
    print("Your vehicle number plate is invalid")
    exit()

if (rounded_up2) <= float(d):#asks the program if the average speed is above or below the speed limit
                print("You have not exceeded the speed limit")
else:
    print("You have exceeded the speed limit")
    print("You will be charged with a £100 fine")
    List_of_avspeed.append (rounded_up2)#this also appends to the list


print(List_of_vehicles)#this outputs the list for vehicle number plates
print(List_of_avspeed)#this outputs the list for average speeds

#below, is where a file is created for the vehicle number plates entered
Speed_limit_file=open(str(List_of_vehicles)+".txt",'w')#creation of file
Speed_limit_file.write("Vehicle number plate is: "+str(List_of_vehicles)+"\n")#adding the number plates
Speed_limit_file.write("Vehicle average speed is: "+str(List_of_avspeed)+"\n")#adding the average speeds
Speed_limit_file.write("The vehicles listed above will receive £100 fine"+"\n")
Speed_limit_file.close()#the file must be closed at the end of the program

## main problem below, on the road list and postcode list
if re.match("[A-Z]{2}[0-9]{2}[A-Z]{3}", v_n_p):

    if rounded_up2 > float(d):

        data_file= open(str(List_of_vehicles)+".txt","w")
        data_file.write("Vehicle number plate: "+str(List_of_vehicles) +"\n")
        data_file.write("Vehicle average speed: "+str(List_of_avspeed) +"\n")

        owners=["Joe","Ken","Jake","Jack"]
        names=random.choice(owners)
        data_file.write=("The owner's name is:"+str(names) +"\n")

        road=["54 Highgate Drive","78 Knighton Road", "67 Beligner Road"]
        roads=random.choice(road)
        data_file.write("The address:"+str(roads) +"\n")

        pstcode=["LE2 6HW","UI3 7GH","OI4 8HG"]
        pstcodes=random.choice(pstcode)
        data_file.write("The Postcode is: "+str(pstcodes) +"\n")

        data_file.close()
    else:
        (" ")
else:
    (" ")

1 个答案:

答案 0 :(得分:1)

我相信你的问题是以下代码行

data_file.write=("The owner's name is:"+str(names) +"\n")

您需要删除'='

data_file.write("The owner's name is:"+str(names) +"\n")

希望这有帮助。