shaywune shaywune - 2 months ago 7
Python Question

Errors running python code, attribute error for datetime and strptime type error

I was given a project to complete:


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)




Now here is where I come up against a brick wall:


  1. I have on my laptop python 2.7.12 while the tutor is using Microsoft Visual Studio 2013 and teaching cpython.

  2. when I run my code it compiles but as soon as I input userdata it gives me the error displayed below.



MY CODE:

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


ERROR GIVEN:

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'



  1. when I tried to run the tutors code, I came up with another error (again these errors are given after accepting input from user.



TUTORS CODE:

#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.")


ERROR GIVEN:

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


Please help me understand why I am getting both errors when I run the two programs and what I must do to correct it.

Answer

Install python3 so that you and your tutor are on the same page.

However, if you do decide to stick with python 2.7, this will fix your problem.

Your problem is in this line

#Ask the user for the date of their deadline
strDeadline = input("Please enter the date of your deadline (mm/dd/yyyy): ")

Here's an example of what I mean

>>> input()
5
5
>>> input()
10/2
5
>>> input()
10/2/2016
0

python is thinking that your date is arithmetic division of integers. change input() to raw_input() to accept the string.

i.e.

strDeadline = raw_input("Please enter the date of your deadline (mm/dd/yyyy): ")
Comments