Python programming help needed

Associate
Joined
28 Oct 2014
Posts
472
Hi I have the following question to answer in python.

Q) On many major roads average speed checks are in place. Two sensors are placed a known distance apart and vehicle number plate recognition is used to identify a vehicle and the time it enters the section of road being monitored. The time is recorded when the vehicle leaves the monitored section. By using the time taken to travel the known distance, the average speed of a vehicle can be calculated. Create a program for calculating average speeds for a vehicle travelling through a section of road. Output a list of those vehicles exceeding the speed limit set for that section of road.

This is my piece of code but I don't know where I went wrong, would someone be able to help me?

from datetime import datetime

timestart=int(input("Please enter the start time in 24 hours time"))
finishtime=int(input("Please enter end time in 24 hour time"))


name=input("Please enter your name")
timestart=datetime.strptime(timestart, "%H:%M")
finishtime=datetime.strptime(finishtime, "%H:%M")
distance=int(input("Please enter the distance in miles"))
timeanswer=finishtime-timestart
timeanswer2=(timeanswer.seconds/3600)
speed=distance/timeanswer2

speed2=speed
print(speed2,"mph")


if speed>10:
cyclists=open("cyclist.txt","a")
cyclists.write(name)
cyclists.write(" ")
cyclists.write(str(speed2))
cyclists.write("mph\n")
cyclists.close()
 
Soldato
Joined
8 May 2011
Posts
4,939
Location
HQ
Your reading in an int, but strptime requires a string. It could also be clearer that the format is 00:00 not 0000 or 00 00 etc.

From the snippet speed2 (making a copy of speed) doesn't achieve anything? But you didn't actually say what the problem you were having when you ran it?

Code:
from datetime import datetime

timestart = input("Please enter the start time in 24 hours time")
finishtime = input("Please enter end time in 24 hour time")

name = input("Please enter your name")
timestart = datetime.strptime(timestart, "%H:%M")
finishtime = datetime.strptime(finishtime, "%H:%M")
distance = int(input("Please enter the distance in miles"))
timeanswer = finishtime - timestart
timeanswer2 = (timeanswer.seconds / 3600)
speed = distance / timeanswer2

print("{} mph".format(speed))

if speed > 10:
    cyclists = open("cyclist.txt", "a")
    cyclists.write(name)
    cyclists.write(" ")
    cyclists.write(str(speed))
    cyclists.write("mph\n")
    cyclists.close()
 
Last edited:
Soldato
Joined
6 Mar 2008
Posts
10,079
Location
Stoke area
are you supposed to be writing a piece of software that you randomly enter data in or have they given you a list of times, number plates and a distance to work from?

Also, when asking for Python help, always include what version you are using, just helps out a little :)

Firstly, inputs need clearing up, extra space at the end makes it look neater. Then i figured that the distance between the cameras wouldn't change while inputting one list of reg plates and times, you also weren't asking for the speed of the road.

This will initially ask for distance as a float not an int because police often use 0.5 miles between cameras, then ask for the road speed limit. You can then enter a reg, times and see the average mph. It asks if you've another to enter and keeps looping as long as you type yes. This needs work in case they type Yes, Y or y. The whole lot needs coding to pick up input mistakes too but I've not got time.

Here is my input in 3.4:
Code:
from datetime import datetime

def speedCalc(timestart, finishtime):
    timestart = datetime.strptime(timestart, "%H:%M")
    finishtime = datetime.strptime(finishtime, "%H:%M")
    timeanswer = finishtime - timestart
    timeanswer2 = (timeanswer.seconds / 3600)
    speed = distance / timeanswer2
    return speed

def writeSpeed(reg, speed):
    if speed > 10:
        speeders = open("speeders.txt", "a")
        speeders.write(reg)
        speeders.write(" ")
        speeders.write(str(speed))
        speeders.write("mph\n")
        speeders.close()
    

distance = float(input("Please enter the distance in miles between the 2 camera's: "))
limit = int(input("What is the speed limit for this road in mph? "))
another = ("yes")


while another == ("yes"):
    reg = input("Please enter the reg: ")
    timestart = input("Please enter the start time in 24 hours time: ")
    finishtime = input("Please enter end time in 24 hour time: ")
    speed = speedCalc(timestart, finishtime)
    print("{} mph".format(speed))
    if speed > limit:
            writeSpeed(reg, speed)
    another = input("Would you like to enter another vehicle, yes or no? ")

http://www.codesend.com/view/a6504561f244fbda7dc92990f1f4aaa3/

My Python is very rusty so this could be improved
 
Last edited:
Associate
OP
Joined
28 Oct 2014
Posts
472
So now this program is all fine and it works:

from datetime import datetime

timestart = input("Please enter the start time in 24 hours time")
finishtime = input("Please enter end time in 24 hour time")

name = input("Please enter your name")
timestart = datetime.strptime(timestart, "%H:%M")
finishtime = datetime.strptime(finishtime, "%H:%M")
distance = int(input("Please enter the distance in miles"))
timeanswer = finishtime - timestart
timeanswer2 = (timeanswer.seconds / 3600)
speed = distance / timeanswer2

print("{} mph".format(speed))

if speed > 10:
cyclists = open("cyclist.txt", "a")
cyclists.write(name)
cyclists.write(" ")
cyclists.write(str(speed))
cyclists.write("mph\n")
cyclists.close()

Now I have got another questions which asks for the following:

In the UK most vehicle registrations are in the format: • two letters • two numbers • three letters. For example, AZ01 XYZ. The vehicle number plate recognition system will provide this information as a string of characters. By identifying any vehicle registrations that do not match this pattern, a list of non-standard vehicle registrations and average speeds in excess of the speed limit should be compiled and saved. Create a program for saving a file with these non-standard registrations for those vehicles exceeding the speed limit set for that section of road.

So I would use code from the first question but I don't know how to do the number plate part. So how to do that it will record the non-standard number plates?
 
Back
Top Bottom