不了解此定时while循环属性错误

时间:2019-12-16 19:16:28

标签: python python-3.x datetime time while-loop

所以我一直在尝试做一个while循环,它将运行10分钟。但是用while循环不断出错。它说'str' object has no attribute 'time'

我发现,如果我删除其中带有now.strftime()的行,则代码会运行,但我不知道为什么没有这些行或如何解决它。

我也确实尝试使用datetime模块来执行某些操作,而不是导入time模块,但这也失败了。

import math
from datetime import datetime
import time


test_num = 1
largest_loop = 0
delay = 60 * 10
end_time = time.time() + delay


def even_number(value):
    if value == 2:
        return True


def divide_five(value):
    if value == 5:
        return True


def is_square(value):
    if math.sqrt(value).is_integer():
        return False


def multiple_of(value):
    if value == 2:
        return True


def is_happy():
    global check
    if check == 1:
        return True


while time.time() <= end_time:
    test_num += 1
    check = test_num
    now = datetime.now()
    loop_counter = 0
    record_loop = 6
    date = now.strftime("%m/%d/%Y")
    time = now.strftime("%H:%M:%S")
    if even_number(test_num) == True:
        if divide_five(test_num) == True:
            if is_square(test_num) == True:                
                for _ in range(record_loop + 4):
                    loop_counter += 1                            
                    if is_happy() == True:
                        if multiple_of(test_num) == True:
                            #print(test_num)
                            record_loop = loop_counter
                            break
                        else:
                            pass
                else:
                    pass
            else:
                pass
        else:
            pass        
    else:
        pass

3 个答案:

答案 0 :(得分:1)

在导入时间库时,不要在while循环时间内命名变量:

time = now.strftime("%H:%M:%S")

在while循环中,您想使用时间库的time函数,但是一旦运行while循环,它将尝试在while循环中定义的字符串时间上使用time()。

答案 1 :(得分:1)

正如@CoryKramer所指出的,您为变量time命名,这也是您要导入的模块的名称。我真正要做的就是将time变量更改为类似currTime的变量。尝试以下代码(适合我的代码):

import math
from datetime import datetime
import time


test_num = 1
largest_loop = 0
delay = 60 * 10
end_time = time.time() + delay


def even_number(value):
    if value == 2:
        return True


def divide_five(value):
    if value == 5:
        return True


def is_square(value):
    if math.sqrt(value).is_integer():
        return False


def multiple_of(value):
    if value == 2:
        return True


def is_happy():
    global check
    if check == 1:
        return True


while time.time() <= end_time:
    test_num += 1
    check = test_num
    now = datetime.now()
    loop_counter = 0
    record_loop = 6
    date = now.strftime("%m/%d/%Y")
    currTime = now.strftime("%H:%M:%S")
    if even_number(test_num) == True:
        if divide_five(test_num) == True:
            if is_square(test_num) == True:                
                for _ in range(record_loop + 4):
                    loop_counter += 1                            
                    if is_happy() == True:
                        if multiple_of(test_num) == True:
                            #print(test_num)
                            record_loop = loop_counter
                            break
                        else:
                            pass
                else:
                    pass
            else:
                pass
        else:
            pass        
    else:
        pass

另外,请考虑阅读以下内容:

答案 2 :(得分:1)

我认为问题出在这里

time = now.strftime("%H:%M:%S")

因此,您将时间转换为字符串变量。以不同的方式命名该变量!

除此之外,在“全油门”下运行10分钟还很多!考虑在while循环结束时引入“睡眠”时间(只是建议)