Date using input

Dave Angel davea at ieee.org
Fri Sep 25 05:50:57 EDT 2009


flebber.crue at gmail.com wrote:
> I don't think I am using re.compile properly, but thought as this 
> would make my output an object it would be better for later, is that 
> correct?
>
> #Obtain date
> def ObtainDate(date):
> date = raw_input("Type Date dd/mm/year: ")
> re.split('[/]+', date)
> date
> year = date[-1]
> month = date[1]
> day = date[0]
> re.compile('year-month-day')
>
>
You persist in top-posting (putting your reply ahead of the sequence of 
messages you're quoting).

Regular expressions are an advanced technique.  You need to learn how 
strings work, how names clash, how functions return values.  Learn how 
the fundamental types can be manipulated before messing with datetime 
module, or re module.  As I said in an earlier message:

 >>Do you understand what kind of data is returned by raw_input() ?  If 
so, look at the available
 >>methods of that type, and see if there's one called split() that you 
can use to separate out the
 >>multiple parts of the user's response.  You want to separate the dd 
from the mm and from the year.

You've done the same with the more complex and slower re module.  You 
could have just used the code I practically handed you:

    date_string = raw_input("Type Date dd/mm/year: ")
    date_list = datestring.split("/")

Then, you have correctly parsed the list into separate string 
variables.  Easier would have been:

    day, month, year = date_list

or just combining the last two lines:
     day, month, year = date_string.split("/")

Now you'd like to put them together in a different order, with a 
different separator.  The re module doesn't do that, but again, you 
don't need it.  Look up str.join(), which is pretty much the inverse of 
split, or simply look up how to concatenate strings with the "+" operator.

Now, consider the calling and return of that function.  What are you 
using the formal parameter "date" for?  Why is it even there?  And what 
value are you intending to return?

Try writing one complete script, complete with a single definition, and 
a single call to that definition, perhaps printing the result calling 
that definition.  Master chapters 1-3  before jumping to chapter 12.

DaveA





More information about the Python-list mailing list