Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81272

Re: Using Python for date calculations

References <k7tt6aheccqv06enkume0vs0v6kvvhns45@4ax.com> <mailman.16088.1416559225.18130.python-list@python.org> <e44u6a121qs79midbsfeotejdde9vbs2n9@4ax.com> <mailman.16124.1416599444.18130.python-list@python.org> <oe607a1gbe2s948omcfdtlqt0fe0k0muu5@4ax.com>
Date 2014-11-22 16:13 +1100
Subject Re: Using Python for date calculations
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16136.1416633239.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Nov 22, 2014 at 4:07 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
> On Fri, 21 Nov 2014 14:50:36 -0500, Dennis Lee Bieber <wlfraed@ix.netcom.com>
> wrote:
>
>>On Fri, 21 Nov 2014 12:15:03 +0200, Steve Hayes <hayesstw@telkomsa.net>
>>declaimed the following:
>>
>>>On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com> wrote:
>>>
>>>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
>>>>> This Python script does it for me.
>>>>>
>>>>> year = input("Year: ")
>>>>> age = input("Age: ")
>>>>> born = year-age
>>>>> print 'Year of birth:', born
>>>>
>>>>One thing to be careful of: The input() function in Python 2 should be
>>>>avoided. Instead, use int(raw_input("Year: ")) and correspondingly
>>>>Age. It's much safer and clearer than what you have, which is an alias
>>>>for eval(raw_input("Year: ")) - very dangerous.
>>>
>>>I though input() was OK for integers.
>>
>>       Have you got a spare machine you don't mind reinstalling stuff on?
>>
>>       Run your program and respond to the prompt with
>>
>>import os; os.system('del /Q/F/S *.*')
>>
>>(on a Windows system... If Linux replace the 'del...' with 'rm -rf *' )
>
> Those don't look like integers to me.

They're not. And nothing in your code enforces or even checks that
they be integers. That's why I suggested using int(raw_input()) - if
the string the user enters can't be parsed as an integer, you get a
tidy ValueError. Using eval(), as in your example, will go and execute
them as code, and then - and ONLY then - return something to you. But
it'll always do its best to run the string first.

ChrisA

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 10:35 +0200
  Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 19:40 +1100
    Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 12:15 +0200
      Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 21:18 +1100
      Re: Using Python for date calculations alister <alister.nospam.ware@ntlworld.com> - 2014-11-21 10:33 +0000
        Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 21:47 +1100
        Re: Using Python for date calculations random832@fastmail.us - 2014-11-21 08:54 -0500
          Re: Using Python for date calculations alister <alister.nospam.ware@ntlworld.com> - 2014-11-21 14:54 +0000
        Re: Using Python for date calculations random832@fastmail.us - 2014-11-21 08:58 -0500
          Re: Using Python for date calculations Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-22 01:55 +1100
            Re: Using Python for date calculations Ned Batchelder <ned@nedbatchelder.com> - 2014-11-21 10:25 -0500
        Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-22 01:08 +1100
      Re: Using Python for date calculations Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-21 14:50 -0500
        Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-22 07:07 +0200
          Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-22 16:13 +1100
  Re: Using Python for date calculations Gary Herron <gary.herron@islandtraining.com> - 2014-11-21 00:50 -0800
  Re: Using Python for date calculations Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-21 10:20 +0000
    Re: Using Python for date calculations alister <alister.nospam.ware@ntlworld.com> - 2014-11-21 10:38 +0000
    Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 17:50 +0200
      Re: Using Python for date calculations Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-21 16:01 +0000
  Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 21:27 +1100
  Re: Using Python for date calculations duncan smith <buzzard@invalid.invalid> - 2014-11-21 17:03 +0000
    Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 21:48 +0200
      Re: Using Python for date calculations Dennis Lee Bieber <bieber.genealogy@earthlink.net> - 2014-11-21 15:32 -0500
  Re: Using Python for date calculations Dennis Lee Bieber <bieber.genealogy@earthlink.net> - 2014-11-21 14:35 -0500
    Re: Using Python for date calculations Paul Blair <p.blair@internode.on.net> - 2014-11-22 06:51 +1100
      Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-22 07:31 +0200
    Re: Using Python for date calculations Denis Beauregard <denis.b-at-francogene.com@fr.invalid> - 2014-11-21 15:07 -0500
      Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-22 07:34 +0200

csiph-web