循环python时钟的问题
我正在制作一个时钟,在连续循环中显示英国和佛罗里达的时间,但当我运行代码时,它只显示我开始运行代码的时间 请记住,这是我的第一个项目。因此,任何业余技巧也会有所帮助 守则本身:循环python时钟的问题,python,time,clock,Python,Time,Clock,我正在制作一个时钟,在连续循环中显示英国和佛罗里达的时间,但当我运行代码时,它只显示我开始运行代码的时间 请记住,这是我的第一个项目。因此,任何业余技巧也会有所帮助 守则本身: #UK Time from datetime import datetime now = datetime.now() current_time = now.strftime("%H:%M:%S") #Florida time from datetime import timedelt
#UK Time
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
#Florida time
from datetime import timedelta
minus_hours = now - timedelta(hours=5)
display_minus_hours = minus_hours.strftime("%H:%M:%S")
while True:
print('UK =', current_time)
print('Florida =', display_minus_hours)
您需要更新循环中的变量
从datetime导入datetime,timedelta
尽管如此:
#英国时间
now=datetime.now()
当前时间=现在。strftime(“%H:%M:%S”)
#佛罗里达时间
减小时=现在-时间增量(小时=5)
显示小时数=小时数。strftime(“%H:%M:%S”)
打印('UK=',当前时间)
打印('Florida=',显示时间减去小时)
P.S.在不改变的情况下打印很多东西,考虑添加<代码>时间。睡眠(1)< /代码>
< P>问题是变量只被赋值一次。< /P>var = datetime.now()
while True:
print(var)
每次迭代都需要更新变量
while True:
var = datetime.now()
print(var)
您必须调用
datetime。现在再次调用。循环中的代码不会更新变量,因此当然会反复显示相同的值。您需要更新循环中current\u time
和display\u-hours
的值。T\u TWelcome到堆栈溢出。如果答案回答了你的问题,不要忘记接受(在答案旁边打勾)。这样,您的问题在搜索中停止显示为未回答。也投票选出好答案。还请注意,接受并向上投票答案是在堆栈溢出时表示感谢的方式(不要为此使用注释)。既然你是从这里开始的,请拿着这个,读一读,看看。非常感谢,这正是它所需要的,是的,我正在增加一个时间。睡眠(1)