如何在Python中修复'NameError'

时间:2019-07-08 12:49:07

标签: python datetime

我有一些代码。当用户在输入行中输入“日期”时,输出必须是日期,例如2019-06-8 16:34:40 但是程序找不到输入文本-未定义名称“日期”

import datetime

x = input("Type your question here ... ")
########## Date ##########
now = datetime.datetime.now()
date = "Date"
if x == date:
  print "Current date and time:"
  print str(now)

########## (Can't find anything) ##########
else:
  print("Something goes wrong ;( ")

错误- NameError:名称“日期”未定义

1 个答案:

答案 0 :(得分:0)

在Python 3中

import datetime

x = input("Type your question here ... ")
########## Date ##########
now = datetime.datetime.now()
date = "Date"
if x == date:
  print ("Current date and time:")
  print (str(now))

########## (Can't find anything) ##########
else:
  print("Something goes wrong ;( ")

,输出为:

Type your question here ... Date                                                                                                                              
Current date and time:                                                                                                                                        
2019-07-08 12:53:41.534617