Python Forum
Creating a program that records speed in a speed trap
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a program that records speed in a speed trap
#1
That is my code so far it says my subtraction is wrong can you point out anything I can do better.

Thanks.

from datetime import datetime
#Function to convert date from string to dateTime
def dateTimeIn(timestamp):
    dateTimeIn = datetime.strptime(strDateIn, "%d/%m/%Y %H:%M%S")
    return dateTimeIn
def dateTimeOut(timestamp):
    dateTimeOut = datetime.strptime(strDateOut, "%d/%m/%Y %H:%M%S")
    return dateTimeOut   

#Main Program   
myFileIn=open("CameraIn.txt","rt")
myFileOutput=open("Output.txt","rt")
for lineIn in myFileIn:
    lineIn=myFileIn.readline()
    theListIn=lineIn.split(",")
    regIn=theListIn[0]
    strTimeIn=theListIn[1]
    myFileOut=open("CameraOut.txt","rt")
    for lineOut in myFileOut:
        lineOut=myFileOut.readline()
        theListOut=lineOut.split(",")
        regOut=theListOut[0]
        strTimeOut=theListOut[1]
        if regIn==regOut:
            dateTimeIn-dateTimeOut
Reply
#2
When you have an error you should include the error traceback.
dateTimeIn and dateTimeOut are function that need to be called by placing () at the end with a timestamp between those brackets.
In the functions the variable names need looking at to match the input variable.
Reply
#3
error traceback, sorted out the names just stuck with subtracting
Error:
Traceback (most recent call last):   File "C:\Users\Aston Instone\Documents\College\IT BTEC\Unit 16\Assignment 2\Python Program\CurrentProgram.py", line 30, in <module>     dateTimeIn-dateTimeOut TypeError: unsupported operand type(s) for -: 'function' and 'function'
>>>

It tells me timestamp is needed so I enter it than says timestamp is not defined?
from datetime import datetime
#Function to convert date from string to dateTime
def dateTimeIn(timestamp):
    dateTimeIn = datetime.strptime(strTimeIn, "%d/%m/%Y %H:%M:%S")
    return dateTimeIn
def dateTimeOut(timestamp):
    dateTimeOut = datetime.strptime(strTimeOut, "%d/%m/%Y %H:%M:%S")
    return dateTimeOut
   

#Main Program   
myFileIn=open("CameraIn.txt","rt")
myFileOutput=open("Output.txt","rt")
for lineIn in myFileIn:
    lineIn=myFileIn.readline()
    theListIn=lineIn.split(",")
    regIn=theListIn[0]
    strTimeIn=theListIn[1]
    myFileOut=open("CameraOut.txt","rt")
    for lineOut in myFileOut:
        lineOut=myFileOut.readline()
        theListOut=lineOut.split(",")
        regOut=theListOut[0]
        strTimeOut=theListOut[1]
        if regIn==regOut:
            dateTimeIn(timestamp)-dateTimeOut(timestamp)
Error:
Traceback (most recent call last):   File "C:\Users\Aston Instone\Documents\College\IT BTEC\Unit 16\Assignment 2\Python Program\CurrentProgram.py", line 30, in <module>     dateTimeIn(timestamp)-dateTimeOut(timestamp) NameError: name 'timestamp' is not defined
>>>

I have to get the timestamp from the file and I want it to do this for every timestamp that shows that the car is speeding?
Reply
#4
Variable names need to match. the ones you pass to a function need to have already been assigned a value. The variable names used in the function that is from a passed in value need to match the function parameters.
def somefunction(variableinside):
   print(variableinside)

Variableoutside = 'testing'
somefunction(variableoutside)
Reply
#5
Is is possible for you to edit my code to work, sorry but I'm not very good at python
Thanks for your help! -This is unfinished and once this is working I will finish it off
Reply
#6
We're here to help people learn Python, not do their work for them. Unfortunately, learning is effortful so you really should try and we help you with any problems you run into, rather than us completing the very end of it for you.
Reply
#7
(Nov-01-2016, 06:36 PM)micseydel Wrote: We're here to help people learn Python, not do their work for them. Unfortunately, learning is effortful so you really should try and we help you with any problems you run into, rather than us completing the very end of it for you.

however, some people are willing to program for money, office space with window views and freebies, etc Dance   we have another forum for posting such opportunities Shocked
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Nov-01-2016, 05:46 PM)astonavfc Wrote:
    myFileOut=open("CameraOut.txt","rt")
    for lineOut in myFileOut:
        lineOut=myFileOut.readline()

Why are you reading from an output file?  Why are you reading each line twice?  Why don't you ever close any of the files you open?
We can't help if the code doesn't make sense.  It's like asking us to help you fix the food you're cooking, after you've already covered it with curry.  Sure it can still work, but why make us do all that extra work?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,813 May-03-2023, 08:22 AM
Last Post: billykid999
  itertools and amazing speed Pedroski55 8 2,044 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  How to speed up nested cycles? alexfrol86 3 1,547 Mar-25-2022, 06:19 AM
Last Post: deanhystad
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,609 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,313 Jan-21-2022, 06:24 PM
Last Post: mcva
  Speed up code with second process help samuelbachorik 0 1,409 Sep-04-2021, 09:31 AM
Last Post: samuelbachorik
  Random coordinate generator speed improvement saidc 0 2,048 Aug-01-2021, 11:09 PM
Last Post: saidc
  creating simplex tableau pivot program easy or difficult for a beginner in Python? alex_0 2 2,569 Mar-31-2021, 03:39 AM
Last Post: Larz60+
  How to create a speed limiter for a car Tyrel 3 2,882 Feb-24-2021, 09:32 AM
Last Post: Tyrel
  Check internet speed ebolisa 1 1,768 Oct-29-2020, 08:09 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020