0

I'm currently writing a program to check speeds of cars and their license plates, and I want to repeat the function that does this x number of times, the problem I'm having though is that the function is repeating endlessly and is not adhering to the number of times I want it to loop. Here is what I have so far:

    if correctMatch:
    pass
else:
    with open('Camera Output.txt', 'a') as f:
        print("DATA RECORDED TO: Camera Output.txt")
        exactTime2 = datetime.now()
        f.write("{} has a non-standard license plate and has been recorded at {}.".format(licensePlate,
                                                                                              exactTime2) + "\n")
        f.write("---------------------------------------------------------\n")
if speedCarMph > 60:
    with open('Camera Output.txt', 'a') as f:
        print("DATA RECORDED TO: Camera Output.txt")
        exactTime= datetime.now()
        f.write("{} was travelling at {}MPH, recorded at {} and has broken the law.".format(licensePlate,
                                                                                                speedCarMph, exactTime) + "\n")
        f.write("----------------------------------------------------------\n")
licensePlateCheck()
for x in range(N):
    repeatNum = 0
    while repeatNum < 10:
        repeatNum += 1
        licensePlateCheck()
    if repeatNum == 10:
        print("Completed generation")

I also attempted to use a thread but that didn't work. If you need any more of the code, just ask. The full code is here (excluding an unrelated function and the function choice):

import re
import threading
from queue import Queue
def licensePlateCheck():
   camInput1 = datetime.now()
   print(camInput1)
   print("Car is travelling...")
   time.sleep(0.1)
   print("Car has passed cam2")
   camInput2 = timedelta(seconds = random.uniform(5, 10))
   distance = 200
   duration = camInput2.total_seconds()
   print("Time Delta is equal to: {0}".format(duration))
   speedCarMs = distance/duration
   print("Car is travelling in m/s at: {0}".format(speedCarMs))
   speedCarMph = 2.237*speedCarMs
   print("Car is travelling in MPH at: {0}".format(speedCarMph))
   licenseCharNum = randint(2,9)
   licensePlate = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(licenseCharNum))
   licensePlateLayout = re.compile('[A-Z][A-Z]\d\d[A-Z][A-Z][A-Z]')
   correctMatch = licensePlateLayout.match(licensePlate)
   if correctMatch:
      pass
   else:
      with open('Camera Output.txt', 'a') as f:
         print("DATA RECORDED TO: Camera Output.txt")
         exactTime2 = datetime.now()
         f.write("{} has a non-standard license plate and has been recorded at {}.".format(licensePlate,
                                                                               exactTime2) + "\n")
        f.write("----------------------------------------------------------\n")
   if speedCarMph > 60:
      with open('Camera Output.txt', 'a') as f:
         print("DATA RECORDED TO: Camera Output.txt")
         exactTime= datetime.now()
         f.write("{} was travelling at {}MPH, recorded at {} and has broken the law.".format(licensePlate,
                                                                                                speedCarMph, exactTime) + "\n")
        f.write("----------------------------------------------------------\n")
   licensePlateCheck()
   for x in range(N):
      repeatNum = 0
      while repeatNum < 10:
         repeatNum += 1
         licensePlateCheck()
      if repeatNum == 10:
         print("Completed generation")
3
  • 3
    I can't quite see the purpose of using a for loop and a while loop. Could you explain why you have used two loops instead of one? What is the 'N' in for x in range(N)? Commented Mar 2, 2015 at 8:39
  • According to your code you want to trigger licensePlateCheck method 1 + x * 10 times? Commented Mar 2, 2015 at 8:40
  • I'd simply like to make a function repeat x number of times. N would represent the number of times that it would repeat, but I was confused, hence my coming here to help. Commented Mar 2, 2015 at 8:40

1 Answer 1

2

In that case you have unnecessary used while loop:)

for x in range(N): // will iterate x times
    licensePlateCheck()

print("Completed generation")

With nested while loop, your method would execute:

x * 10 times:

  • x - for loop
  • 10 - while loop

Both For and While are correct, the choice is up to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.