Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复';名称错误';用Python_Python_Datetime - Fatal编程技术网

如何修复';名称错误';用Python

如何修复';名称错误';用Python,python,datetime,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:"

我有一些密码。当用户在输入行中写入“日期”时,输出必须是日期,如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:Python 3中未定义名称“date”

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 ;( ")

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     

您使用的是python 2还是python 3?请尝试将if条件更改为
if x=='Date':
对于python 2,将
输入更改为
原始输入,对于python 3,将
打印更改为使用
()
工作您确定这就是您正在运行的代码吗?现在,该
if
语句应该是有效的(尽管缺少括号)@depperm我使用py2v。