python:尝试在循环内添加另一个 "timered" 打印函数,但收效甚微 :D
python: Trying to add another "timered" print function inside a loop, with not great success :D
第一部分被删减了。很抱歉问这个问题,但我对 python 和一般编程还不熟悉。我的知识和经验不足,我并不总是能够找到(或理解)我需要做什么才能取得成就。
现在我每 2 秒执行一次 print('2')
,每 30 秒执行一次 print('30')
。
所以现在我想实现另一个函数 print('10')
在循环中每 10 秒执行一次。
你能帮我理解如何实现这个吗?
SHORT_DELAY = 2
LONG_DELAY = 30
accum_time = LONG_DELAY
while True:
print('2')
if accum_time > LONG_DELAY:
print('30')
accum_time -= LONG_DELAY
time.sleep(SHORT_DELAY)
accum_time += SHORT_DELAY
哇,现在怎么办??
为了实现我的目标,我会说我需要设置另一个 var,例如 MID_DELAY = 10
,但是我如何在循环中实现这个新参数?
我无法解决这个简单的任务,因为如果我重置 mid_delay
我永远不会让 long_delay
值大于 30,如果我重置 long_delay
mid_delay
实际上只有在 long_delay
被重置后才有效,所以它只会在 30 秒被重置后打印“10”。
同样使用这样的代码,几秒钟后我得到了“30”的打印结果。 (我认为这是因为循环以 accum_time
开始,其值已经是“30”,所以一旦它变大就会打印出来)
希望我的问题很清楚。
对不起,如果有混淆,我在写这篇文章时试图“调试”,但仍然没有成功..
原谅我小白
我可以帮你,
我稍微更改了代码,以便更容易阅读。
import time
SHORT_DELAY = 2
while True:
print('2')
SHORT_DELAY += 1
time.sleep(2)
#when 30 seconds is full it prints 30
if SHORT_DELAY==31:
SHORT_DELAY=0
print('30')
when 10 seconds is full it prints 10
if SHORT_DELAY==11 or 21 or 31:
print('10')
希望对您有所帮助。如果有问题或难以理解,请给我留言。
在您的问题中没有必要使用“for”循环。开始思考“如何解决我的问题?”你想让我做什么?
你想在适当的时间间隔打印一些东西。你想在你的变量中设置这个间隔。
所以开始计时,用函数IF
检查是否已经达到数值
import time
SHORT_DELAY = 2
MID_DELAY = 10
LONG_DELAY = 30
accum_time = 0
while True:
if accum_time == 0:
pass
elif accum_time % LONG_DELAY == 0:
print('30')
elif accum_time % MID_DELAY == 0:
print('10')
else accum_time % SHORT_DELAY == 0:
print('2')
time.sleep(SHORT_DELAY)
accum_time += SHORT_DELAY
当然,当 accum_time 增加 SHORT_DELAY 时,您不必检查“else”中的语句。
else:
print('2')
第一部分被删减了。很抱歉问这个问题,但我对 python 和一般编程还不熟悉。我的知识和经验不足,我并不总是能够找到(或理解)我需要做什么才能取得成就。
现在我每 2 秒执行一次 print('2')
,每 30 秒执行一次 print('30')
。
所以现在我想实现另一个函数 print('10')
在循环中每 10 秒执行一次。
你能帮我理解如何实现这个吗?
SHORT_DELAY = 2
LONG_DELAY = 30
accum_time = LONG_DELAY
while True:
print('2')
if accum_time > LONG_DELAY:
print('30')
accum_time -= LONG_DELAY
time.sleep(SHORT_DELAY)
accum_time += SHORT_DELAY
哇,现在怎么办??
为了实现我的目标,我会说我需要设置另一个 var,例如 MID_DELAY = 10
,但是我如何在循环中实现这个新参数?
我无法解决这个简单的任务,因为如果我重置 mid_delay
我永远不会让 long_delay
值大于 30,如果我重置 long_delay
mid_delay
实际上只有在 long_delay
被重置后才有效,所以它只会在 30 秒被重置后打印“10”。
同样使用这样的代码,几秒钟后我得到了“30”的打印结果。 (我认为这是因为循环以 accum_time
开始,其值已经是“30”,所以一旦它变大就会打印出来)
希望我的问题很清楚。 对不起,如果有混淆,我在写这篇文章时试图“调试”,但仍然没有成功.. 原谅我小白
我可以帮你, 我稍微更改了代码,以便更容易阅读。
import time
SHORT_DELAY = 2
while True:
print('2')
SHORT_DELAY += 1
time.sleep(2)
#when 30 seconds is full it prints 30
if SHORT_DELAY==31:
SHORT_DELAY=0
print('30')
when 10 seconds is full it prints 10
if SHORT_DELAY==11 or 21 or 31:
print('10')
希望对您有所帮助。如果有问题或难以理解,请给我留言。
在您的问题中没有必要使用“for”循环。开始思考“如何解决我的问题?”你想让我做什么? 你想在适当的时间间隔打印一些东西。你想在你的变量中设置这个间隔。 所以开始计时,用函数IF
检查是否已经达到数值import time
SHORT_DELAY = 2
MID_DELAY = 10
LONG_DELAY = 30
accum_time = 0
while True:
if accum_time == 0:
pass
elif accum_time % LONG_DELAY == 0:
print('30')
elif accum_time % MID_DELAY == 0:
print('10')
else accum_time % SHORT_DELAY == 0:
print('2')
time.sleep(SHORT_DELAY)
accum_time += SHORT_DELAY
当然,当 accum_time 增加 SHORT_DELAY 时,您不必检查“else”中的语句。
else:
print('2')