【发布时间】:2017-04-02 12:18:34
【问题描述】:
我一直在尝试运行一个将日、月、年转换为日期格式的 python 脚本。
我尝试了以下脚本;
# dateconvert2.py
# Converts day month and year numbers into two date formats
def main():
# get the day month and year
day, month, year = eval(input("Please enter day, month, year numbers: "))
date1 = str(month)+"/"+str(day)+"/"+str(year)
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December"]
monthStr = months[month-1]
date2 = monthStr+" " + str(day) + ", " + str(year)
print("The date is", date1, "or", date2+ ".")
main()
结果应该是这样的;
>>> Please enter day, month, and year numbers: 24, 5, 2003
The date is 5/24/2003 or May 24, 2003.
当我运行程序时,出现了一个错误,说明该行;
monthStr = months[month-1]
出现索引错误。
我可以做些什么来改善这一点?请帮忙
【问题讨论】:
标签: python python-3.x