【问题标题】:Restarting a program within itself?重新启动一个程序本身?
【发布时间】:2015-05-28 08:52:17
【问题描述】:
from datetime import datetime
platenumber = input("Enter the license plate number:")
start = input("Press enter to start.")
starttime =(datetime.now())
stop = input("Press enter to stop.")
stoptime =(datetime.now())
dist1 = 0
dist2 = input("Enter the distance of the road in metres:")

time = ((stoptime - starttime).total_seconds())
print((time),"is the time in seconds.")
distance = int(dist2) - dist1
print((distance),"is the the distance in metres.")
speed = float(distance)//time
print((speed),"is the speed of the vehicle in m/s.")

我想在使用后重新启动程序,以便可以多次检查车辆的速度。

请帮我完成代码,以便我可以自行重新启动代码并检查多辆车的速度。

【问题讨论】:

  • 帮我编写可以帮助代码像连续循环一样在自身内部重新启动的代码
  • 为什么不直接使用while 循环?
  • “给我代码” - 不,做你自己的 GCSE 课程。
  • 让程序反复重启本身几乎肯定是错误的方法。我建议学习循环。

标签: python python-2.7 python-3.x


【解决方案1】:

使用while 循环。

while True:
    # Do your code

最后:

    again = input("Do this again?")
    again = again.lower()
    if again.startswith('n'):
        break # Get out of the loop

【讨论】:

  • 它说 break 在循环之外
  • 你把整个东西都放在循环里了吗?
  • 它说打破外部循环
  • 我不会给你写代码的!我愿意帮助你,但我不会仅仅编写你的整个程序自下而上。
  • 感谢您的支持
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 2012-10-27
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
相关资源
最近更新 更多