Displaying time in Python using "Strptime()" -


i'm trying make basic program shows how many days left until next birthday, when run in terminal, says input value should returning string not integer? when using input function in python, doesnt automatically return string value?

    import datetime     currentdate = datetime.date.today()      userinput = input("please enter birthday(mm.dd.yy)")     birthday = datetime.datetime.strptime(userinput, "%m/%d/%y")     print(birthday) 

  1. use %y (small letter) catch 2 digit years
  2. you should advice user expected format - expect "mm/dd/yy", tell user type "mm.dd.yy"

to calculate difference use (for python3, python2 use raw_input jahangir mentioned):

import datetime today = datetime.date.today()  userinput = input("please enter birthday(mm/dd):") birthday = datetime.datetime.strptime(userinput, "%m/%d") birthday = datetime.date(year=today.year, month=birthday.month, day=birthday.day)  if (birthday-today).days<0:     birthday = datetime.date(year=today.year+1, month=birthday.month, day=birthday.day)  print("%d days next birthday!"%(birthday-today).days) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -