Python 如何在循环中获取当前日期时间
开始。。对不起,我的英语很差 我有一些python代码Python 如何在循环中获取当前日期时间,python,Python,开始。。对不起,我的英语很差 我有一些python代码 def come(): 答案=输入('是或否:')。条带() str_datetime=now.strftime(“%Y%m%d%H%m%S”) 如果答案=‘是’: 打印('Okay..{}'。格式(答案)) 打印(str_datetime) 其他: 打印('请再次输入是或否!') 来 来 问题是。。我想得到str\u datetime总是重新加载(总是根据经过的时间进行更改..我的意思是)如果您现在在come函数中初始化datetime
def come():
答案=输入('是或否:')。条带()
str_datetime=now.strftime(“%Y%m%d%H%m%S”)
如果答案=‘是’:
打印('Okay..{}'。格式(答案))
打印(str_datetime)
其他:
打印('请再次输入是或否!')
来
来
问题是。。我想得到str\u datetime总是重新加载(总是根据经过的时间进行更改..我的意思是)如果您现在在
come
函数中初始化datetime
的方法,
它将在每次调用时更新。你能试试下面的吗
from datetime import datetime
def come():
now = datetime.now() # Initialize within the function
answer = input('yes or no : ').strip()
str_datetime = now.strftime("%Y%m%d%H%M%S")
if answer == 'yes':
print('Okay.. {}'.format(answer))
print(str_datetime)
else:
print('Please input yes or no again!')
come()
come()
如果在come
函数中初始化datetime
的now
方法,
它将在每次调用时更新。你能试试下面的吗
from datetime import datetime
def come():
now = datetime.now() # Initialize within the function
answer = input('yes or no : ').strip()
str_datetime = now.strftime("%Y%m%d%H%M%S")
if answer == 'yes':
print('Okay.. {}'.format(answer))
print(str_datetime)
else:
print('Please input yes or no again!')
come()
come()
Ooopss。。我忘记了变量now=datetime.now()是从def中输出的。。谢谢好的,很乐意帮忙。哦。。我忘记了变量now=datetime.now()是从def中输出的。。谢谢好的,很乐意帮忙。