Python For循环无休止地重复



我目前正在编写一个程序来检查汽车及其牌照的速度,我想重复这个函数x次,但我遇到的问题是,这个函数无休止地重复,并且没有遵守我希望它循环的次数。以下是我目前所拥有的:

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

我也尝试过使用线程,但没有成功。如果您需要更多的代码,只需询问即可。完整的代码在这里(不包括不相关的功能和功能选择):

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]dd[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")

在这种情况下,您使用了不必要的while循环:)

for x in range(N): // will iterate x times
    licensePlateCheck()
print("Completed generation")

使用嵌套while循环,您的方法将执行:

x*10次:

  • x-用于循环
  • 10-while循环

For和While都是正确的,选择取决于您。

最新更新