【问题标题】:Trying to create a simple time and date program, having problems with usage of .upper()尝试创建一个简单的时间和日期程序,使用 .upper() 时遇到问题
【发布时间】:2014-07-15 05:35:06
【问题描述】:
from datetime import datetime
now = datetime.now()

user_input = input("Would you like to know the date and time today?").upper()


if user_input == "YES":
    print "The date and time is: " + "%s/%s/%s - %s:%s:%s" % (now.day, now.month,     now.year, now.hour, now.minute, now.second)

我正在尝试让 user_input 全部变为大写,因此我不必包含以下代码:

if user_input == "yes" or "YES" or "YeS" ... etc.

非常感谢任何帮助!

【问题讨论】:

  • 你的问题陈述是什么?在input 声明之后user_input 的值是多少?
  • 您想知道今天的日期和时间吗?是 回溯(最近一次通话最后一次):文件“Time_and_Date.py”,第 4 行,在 user_input = input("Would you like知道今天的日期和时间吗?").upper() 文件“”,第 1 行,在 NameError: name 'yes' is not defined\ 当我输入 yes 作为输入时会发生什么。 ..

标签: python datetime python-3.x


【解决方案1】:

您正在混合使用 Python 2 和 Python 3。将您的 input 更改为 raw_inputinput 适用于 Python 3。

如果你想使用 Python 3,你需要将你最后的 print 语句更改为 function call

print("The date and time is: " + "%s/%s/%s - %s:%s:%s" % (now.day, now.month, now.year, now.hour, now.minute, now.second))

如果你想使用 Python 2,将 input 更改为 raw_input 就足够了。

要确定您正在运行哪个版本,请从控制台执行以下命令:

python --version

【讨论】:

  • 谢谢!有没有办法检查我正在使用哪个?另外,python 2 中的哪一部分,3 中的哪一部分?
  • 我已修改答案以提供更多详细信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2020-01-18
  • 2014-11-18
  • 1970-01-01
相关资源
最近更新 更多