gpt4 book ai didi

python - 看不懂这个定时while循环属性报错

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:20 25 4
gpt4 key购买 nike

所以我一直在尝试制作一个将运行 10 分钟的 while 循环。但是在 while 循环中一直出错。它说 'str' 对象没有属性 '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

最佳答案

正如@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

此外,考虑阅读:

关于python - 看不懂这个定时while循环属性报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com