Tuesday 15 April 2014

debugging - How can I find the difference in years between two dates using Python? (Work out if a person is over 18) -



debugging - How can I find the difference in years between two dates using Python? (Work out if a person is over 18) -

my question if can please help me debug piece of code:

import datetime print ("what date of birth? ") dateofbirth = input("please type date of birth in yyyy-mm-dd format ") year, month, day = map(int, dateofbirth.split('-')) dateofbirth1 = datetime.date(year, month, day) today = datetime.date.today() open('dateutil.tar').read() dateutil.relativedelta import relativedelta difference_in_years = relativedelta(today, dateofbirth1).years if difference_in_years < 18 print ("sorry, not eligible vote.") else print ("you on 18 , eligible vote.")

my objective seek , write piece of code can work out if on 18 , eligible vote. meant achieved asking person input date of birth , working out difference in years between birthdate , today's date, using if statement tell them whether or not able vote (i.e. if difference in years more or less 18).

currently having several problems debugging code. firstly, on line 10 there syntax error unsure how rectify. secondly, if remove lastly 4 lines , run code 1 time again next error:

traceback (most recent phone call last): file "c:\removed\canyouvote.py", line 8, in <module> open('dateutil.tar').read() file "c:\program files (x86)\python\lib\encodings\cp1252.py", line 23, in decode homecoming codecs.charmap_decode(input,self.errors,decoding_table)[0] unicodedecodeerror: 'charmap' codec can't decode byte 0x81 in position 5: character maps <undefined>

however in probability there other mistakes unable pick up. sadly new programming, knowledge , experience not great , help appreciated! while trying research solution have attempted utilize coding unfamiliar with, please right me wrong.

thank much in advance!

the reason unicodedecodeerror you're trying open , read tarball—that is, binary file—as if text file.

when that, python tries interpret arbitrary bytes of file if represented characters in default character set (cp1252), that's going give exception if you're lucky, or give finish garbage if you're not. seek opening dateutil.tar in text editor see how meaningful text.

it's hard how prepare that, because it's not clear why you're trying open , read file in first place. jonrsharpe points out, you're not doing results. , can't imagine would them.

if you're trying create dateutil importable, way not tarball in script, install module, outside script, before running it. simplest way pip install dateutil, automatically find right version of dateutil, download it, unpack it, , install of scripts use.

that beingness said, there's no need dateutil here. if subtract 2 datetime objects, timedelta object.

meanwhile, syntaxerror comes code:

if difference_in_years < 18 print ("sorry, not elegible vote.") else print ("you on 18 , elegible vote.")

in python, compound statements if , else need colon before suite, , suite has indented. see first steps towards programming section of tutorial. so:

if difference_in_years < 18: print("sorry, not eligible vote.") else: print("you on 18 , eligible vote.")

(also notice i've removed space before parentheses, fit pep 8 style, , spelled "eligible" properly.)

python debugging datetime if-statement syntax-error

No comments:

Post a Comment