我目前正在编写一个程序来检查汽车及其车牌的速度,我想重复执行这个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]\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")
答案 0 :(得分:2)
在这种情况下,你有不必要的使用while循环:)
for x in range(N): // will iterate x times
licensePlateCheck()
print("Completed generation")
使用嵌套的while循环,您的方法将执行:
x * 10次:
For和While都是正确的,选择取决于你。