Using Python for date calculations

Chris Angelico rosuav at gmail.com
Fri Nov 21 11:18:17 CET 2014


On Fri, Nov 21, 2014 at 9:15 PM, Steve Hayes <hayesstw at telkomsa.net> wrote:
> On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav at gmail.com> wrote:
>
>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw at 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.

In Py2, input() is basically not OK for anything. On the (extremely!)
rare occasions when you actually want to eval() something the user
types, it's better to be explicit: eval(raw_input()).

ChrisA



More information about the Python-list mailing list