每分钟打印一封信

2024-10-02 12:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想在学校解决这个问题。我试着每分钟打印出x,每十分钟打印一行。到目前为止,我不能得到“打印x”每分钟下来。有人能帮忙吗。 这是我的密码

import time;
inTime = float(input("type in how many second"))
oldTime = time.time()-inTime


print (time.time())

def tenMin(oldTime):
    newTime = time.time()
    if ((newTime - oldTime)>= 25):
        return True
    else:
        False

while (True):
        if (tenMin==True):
            print ("x")
            newTime = time.time()
            oldtime = time.time()
else:
    oldTime = time.time()
    continue

Tags: importtrue密码inputiftimetypefloat
2条回答

首先,您的代码有一些问题:

  1. else: False-这在python中不是正确的语法。

  2. 如果你想要定时器,为什么要请求用户输入?

  3. 你有一个逻辑问题:

    inTime = float(input("type in how many second"))

    oldTime = time.time()-inTime

    你知道吗时间。时间float是的,但是用户真的知道在UnixTime中打印什么吗?

我会建议一个简单的解决方案,虽然不是最好的,但确实有效。 每1分钟打印一次“x”,10分钟后打印“\n”(新行)

import time

def main():

    #both timers are at the same start point
    startTimerTen = time.time()
    startTimerMin = startTimerTen

    while True:
        getCurrentTime = time.time()
        if getCurrentTime - startTimerTen >= 600:
            # restart both parameters
            startTimerTen = getCurrentTime
            startTimerMin = getCurrentTime
            print "This in 10 min!\n"
        if getCurrentTime - startTimerMin >= 60:
            # restart only min parameter
            startTimerMin = getCurrentTime
            print "x"


   #end of main
if __name__ == "__main__":
    main()

你的第一个问题是排队

if (tenMin==True):

如果将函数引用与布尔值进行比较,那么答案显然是错误的。必须传递一个参数

if (tenMIn(oldTime)):

。。。你知道吗

相关问题 更多 >

    热门问题