Forum Archive

[SOLVED] Help with int() error (short code)

Pythonistapro777

Ok, so I'm making something that counts down the number of days to an event.

Here's the code to the one that works (without user input):


import datetime
today = datetime.date.today()
someday = datetime.date(2015,8,24)
diff = someday - today
print(diff.days)

Here's the one that doesn't work (with user input):


import datetime
import console
today = datetime.date.today()
find = console.input_alert('Date', 'Please enter the date you would like to countdown to.\ni.e. 2009 (year),6 (month),29 (day)', '', 'Enter')
someday = datetime.date(find)
diff = someday - today
print(diff.days)

Thanks in advance!

omz

console.input_alert returns a string, but you need three numbers. You could convert it like this:

# ...
try:
  year, month, day = [int(s.strip()) for s in find.split(',')]
  someday = datetime.date(year, month, day)
  diff = someday - today
  print(diff.days)
except ValueError:
  print 'Incorrect date format (must be "year, month, day")'
Pythonistapro777

Thanks a lot @omz!