我需要能够在条件下循环这段代码,如果用户想重复这个程序,那么它必须被循环。如果用户想退出程序,那么所有超速行驶的车牌号都应该显示出来。到目前为止,这就是我所拥有的:
import time
capturedtime=time.time()
speed_limit = 30
distance = 50
numberplates = []
newplate = ("Input number plate")
input("Press enter to start")
start_time = time.time()
input("Press enter to stop")
stop_time = time.time()
capturedtime = stop_time - start_time
print('Time taken: {:.2f} seconds'.format(capturedtime))
print('Distance: {:.2f}'.format(distance))
speed = distance / capturedtime
print('Speed: {:.2f}'.format(speed))
if speed > speed_limit:
print("You were breaking the speed limit")
numberplates.append(newplate)发布于 2015-11-30 10:55:29
如果我正确地理解了您,您就可以将所有代码放入一个while循环中,并通过另一个输入()停止它。
示例:
import time
exit = True
'''your variables'''
while(exit):
'''your Code'''
x = input("Enter 'y' if you want to exit.")
if x.lower() == y:
exit = False
print("All speeding Cars:") # Print all the Cars!
for plate in numberplates:
print(plate)发布于 2015-11-30 10:49:17
我不太确定这是否是你正在寻找的,但尝试以下几个。
import time
capturedtime = time.time()
speed_limit = 30
distance = 500
numberplates = []
loops = True
while(loops):
try:
newplate = input("Input number plate: ")
input("Press enter to start: ")
start_time = time.time()
input("Press enter to stop: ")
stop_time = time.time()
capturedtime = stop_time - start_time
print('Time taken: {:.2f} seconds'.format(capturedtime))
print('Distance: {:.2f}'.format(distance))
speed = distance / capturedtime
print('Speed: {:.2f}'.format(speed))
if speed > speed_limit:
print("You were breaking the speed limit")
numberplates.append(newplate)
exit = input("Press x to exit, Press enter to continue: ")
if exit.lower() == 'x':
loops = False
except Exception:
pass
print(numberplates)我刚在while循环中使用了您的代码。请查看check循环上的文档以获得更多信息,https://wiki.python.org/moin/WhileLoop。
https://stackoverflow.com/questions/33996208
复制相似问题