这个问题真的解释了我的问题,尝试编写一个程序,可以分割日期但我一直遇到同样的错误
def date2():
date = input("Please enter the date in the form of mm/dd/yyyy")
return date
def month(date):
month = int(date[0:3])
return month
def day2(date):
day = int(date[3:5])
return day
def year2(date):
year = int(date[5:])
return year
def main():
date2()
month(date)
day2(date)
year2(date)
print(month + day + year)
main()
Please enter the date in the form of mm/dd/yyyy 12/12/2017
Traceback (most recent call last):
File "H:\DateWithoutSplit.py", line 19, in <module>
main()
File "H:\DateWithoutSplit.py", line 15, in main
month(date)
NameError: name 'date' is not defined
>>>
答案 0 :(得分:1)
def main():
date = date2()
...
归档后,您没有将date
变量存储在任何位置
答案 1 :(得分:0)
你所尝试的是一种不好的做法,该程序也有其他几个错误。 尝试下面的一个,它会给你正确的输出。并尝试学习python strptime和strftime方法。
from datetime import datetime
def main():
date = raw_input("Please enter the date in the form of mm/dd/yyyy")
date = datetime.strptime(date, "%m/%d/%Y")
print(date.month + date.day + date.year)
main()