omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


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

    Pythonista
    2
    3
    1369
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Pythonistapro777
      Pythonistapro777 last edited by

      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):
      <pre><code>
      import datetime
      today = datetime.date.today()
      someday = datetime.date(2015,8,24)
      diff = someday - today
      print(diff.days)
      </code></pre>

      Here's the one that doesn't work (with user input):
      <pre><code>
      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)
      </code></pre>

      Thanks in advance!

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        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")'
        
        1 Reply Last reply Reply Quote 0
        • Pythonistapro777
          Pythonistapro777 last edited by

          Thanks a lot @omz!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors