thomas
thomas

Reputation: 3

How to correct this str error?

i have been trying to create a program to record the speed of a vehicle and take in the vehicle number plate. i have tried to create a file but i have an error: 'str is not callable', i have tried everything i know but i can't seem to get it to work. if you can fix this i will really appreciate it :)

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:
    (" ")

Upvotes: 0

Views: 236

Answers (1)

jharrison12
jharrison12

Reputation: 158

I believe your problem is with the following line of code

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

You need to remove the '='

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

Hope this helps.

Upvotes: 1

Related Questions