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)
- use
%y
(small letter) catch 2 digit years - 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
Post a Comment