我必须为学校制定一个程序,与用户讨论某些事情。它应该运行至少3分钟。我想实现一些在3分钟后停止程序的东西。
在 PROGRAM_DURATION
中指定的秒数后,我的代码似乎没有结束程序import time
start = time.time()
PROGRAM_DURATION = 180
x = 1
while x == 1:
if time.time() < start + PROGRAM_DURATION:
print("Hello")
chat()
else:
print("We're out of time")
break
我尝试从代码中删除 chat(),但程序在给定时间后仍未完成
修改 这是从我的程序粘贴的确切代码副本: 进口时间
start = time.time()
PROGRAM_DURATION = 5 #set to 5 to check if it works
x = 1
while x == 1:
if time.time() < start + PROGRAM_DURATION:
print("greeting")
print("What is your name")
name = input()
print("Hi,",name,"nice to meet you")
print("Lets talk")
chat() #function which lets the user chose what the subject will be
else:
print("Well,",name,"we have run out of time")
print("Thanks for talking to me")
break