首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >速度传感器程序,循环,python 3,A级

速度传感器程序,循环,python 3,A级
EN

Stack Overflow用户
提问于 2015-11-30 10:19:55
回答 2查看 217关注 0票数 1

我需要能够在条件下循环这段代码,如果用户想重复这个程序,那么它必须被循环。如果用户想退出程序,那么所有超速行驶的车牌号都应该显示出来。到目前为止,这就是我所拥有的:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-30 10:55:29

如果我正确地理解了您,您就可以将所有代码放入一个while循环中,并通过另一个输入()停止它。

示例:

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2015-11-30 10:49:17

我不太确定这是否是你正在寻找的,但尝试以下几个。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33996208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档