Quick help for a python newby, please

Wildman best_lay at yahoo.com
Wed Nov 23 18:02:32 EST 2016


On Wed, 23 Nov 2016 06:18:38 -0800, jones.dayton wrote:

> I'm just learning, so please excuse my ignorance for
> what I know is a simple issue...
> 
> I'm writing a "Hello, World" type of script to see how
> things work in python3.  I'm asking for input to get a
> person's birthday, then I want to compare that to
> "today" and see how many days have past.
> 
> --code--
> from datetime import datetime, date
> 
>     person = input("Enter your name: ")
>     bmonth = int(input("Enter the numerical month you were born: "))
>     bday = int(input("Enter the numerical day of the month you were born: "))
>     byear = int(input("Enter the year you were born: "))
> 
>     today = date.today()
> -- end --
> 
> I know to get what I want, I need to do something like:
> d0 = date(2008, 8, 18)
> d1 = date(2008, 9, 26)
> delta = d0 - d1
> print(delta.days)
> 
> 
> but how do I replace the "2008, 8, 18" and "2008, 9, 26" with *my* values?  I've tried several things (which I can't remember all of) but usually end up with an error like this:
> 
>    ...
>     delta = (b - a)
> TypeError: unsupported operand type(s) for -: 'str' and 'str'

Try the code that is below:

import datetime
from datetime import date

today = date.today()
person = input("Enter your name: ")
byear = raw_input("Enter the four-digit year you were born: ")
bmonth = raw_input("Enter the month (1-12)you were born: ")
bday = raw_input("Enter the day (1-31) you were born: ")
try:
    birthdate = date(int(byear), int(bmonth), int(bday))
    dif = (today - birthdate).days
    print person + "'s birthday was " + str(dif) + " days ago."
except ValueError:
    print "The date you entered is not valid!"

-- 
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!



More information about the Python-list mailing list