我要完成一个项目:
Help determine how much time someone has left to meet a deadline
- Ask a user to enter the deadline for their project
- Tell them how many days they have to complete the project
- For extra credit, give them the answer as a combination of weeks & days (Hint: you will need some of the math functions from the module on numeric values)
现在我碰到一堵砖墙:
我的笔记本电脑上有python2.7.12,导师正在使用microsoftvisualstudio2013教cpython。
当我运行我的代码,它编译,但一旦我输入用户数据,它给我下面显示的错误。
我的代码:
import datetime
currentday=datetime.date.today()
#set variable to recieve deadline for project
deadLine = 0
deadLine = raw_input('when is the deadline for your project? (dd/mm/YYYY) ')
deadLine=datetime.dateime.strptime(deadLine, '%d/%m/%Y').date()
daysLeft= deadLine-currentday
print 'Number of days left for your project is : '
print daysLeft
给出的错误:
when is the deadline for your project? (dd/mm/YYYY) 21/10/2016
Traceback (most recent call last):
File "C:\Users\Oluwaseun Okungbowa\Desktop\Video editing and python programming\projectdeadline.py", line 7, in <module>
deadLine=datetime.dateime.strptime(deadLine, '%d/%m/%Y').date()
AttributeError: 'module' object has no attribute 'dateime'
导师代码:
#import the datetime class
import datetime
#declare and initialize variables
strDeadline = ""
totalNbrDays = 0
nbrWeeks = 0
nbrDays = 0
#Get Today's date
currentDate = datetime.date.today()
#Ask the user for the date of their deadline
strDeadline = input("Please enter the date of your deadline (mm/dd/yyyy): ")
deadline = datetime.datetime.strptime(strDeadline,"%m/%d/%Y").date()
#Calculate number of days between the two dates
totalNbrDays = deadline - currentDate
#For extra credit calculate results in weeks & days
nbrWeeks = totalNbrDays.days / 7
#The modulo will return the remainder of the division
#which will tell us how many days are left
nbrDays = totalNbrDays.days%7
#display the result to the user
print("You have %d weeks" %nbrWeeks + " and %d days " %nbrDays + "until your deadline.")
给出的错误:
Please enter the date of your deadline (mm/dd/yyyy): 10/21/2016
Traceback (most recent call last):
File "C:\Users\Oluwaseun Okungbowa\Desktop\Video editing and python programming\projectdeadlineteachers.py", line 16, in <module>
deadline = datetime.datetime.strptime(strDeadline,"%m/%d/%Y").date()
TypeError: strptime() argument 1 must be string, not int
请帮助我理解为什么我在运行这两个程序时同时出现这两个错误,以及我必须做些什么来纠正它。你知道吗
你的问题就在这条线上
这里有一个例子来说明我的意思
python认为日期是整数的算术除法。将
input()
更改为raw_input()
以接受字符串。你知道吗即
第一个错误是拼写错误(datetime不是dateime,检查一下你自己,尽量不要问stackoverflow简单的问题)。 第二个错误:
请参阅: about datetime.strptime()
strtime的第一个参数应该是string(error就是这么说的,errors可以帮助您更好地学习) 因此,请提出第一个论点 输入是
编辑: 我忽略了逻辑错误,因为strDeadLine是int。 问题的出现是因为IDLE执行操作(在本例中是在将其提供给代码之前进行除法)。使用Shell或cmd时不会出现这种情况。我同意我之前所说的,最好的学习方法不是堆积如山,而是自己去尝试。 感谢马蒂恩·皮特斯的评论。你知道吗
相关问题 更多 >
编程相关推荐