【发布时间】:2020-04-09 06:59:40
【问题描述】:
所以我一直在尝试制作一个运行 10 分钟的 while 循环。但是在 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
【问题讨论】:
-
time = now.strftime("%H:%M:%S")你正在用你的变量名隐藏time模块
标签: python python-3.x datetime time while-loop