[Tutor] Future Appointments...

Sander Sweers sander.sweers at gmail.com
Sun Jan 31 17:17:21 CET 2010


On 31 January 2010 16:40, Ken G. <beachkid at insightbb.com> wrote:
> Below is a program to determine when my next appointment is.  Line numbers
> are provided for reference only.
>
> 01  import time, datetime, sys
> 02  from datetime import date
> 03  today = date.today()
> 04  print
> 05  print "Today date is:", today
> 06  todaystr = str(today)

You should use today.strftime(), see [1] for the format.

> 07  print
> 08  print "Corrected date format is:",
> 09  print todaystr[ 5: 7], todaystr[ 8:10], todaystr[ 0: 4]

The above then would be. today.strftime('%Y-%m-%d').

> 10  print
> 11  future = raw_input("Next appointment is in how many days?  "),

This will give a string which will need to be converted to a int. You
can do this with int(future). For example d = int(future).

But this might fail if future is a string like b, $ or other non
number string. You can catch the error, see [2] how to do this.

> 12  print
> 13  difference1 = datetime.timedelta(days=1)

When you converted future to an int variable d replace days=1 with days=d.

> 14  datestring = str(today + difference1)

You can use strftime() above.

> 15  print "Next appointment after that is:",datestring
> 16  print
> 17  print "Corrected date format for next appointment is:",
> 18  print datestring[ 5: 7], datestring[ 8:10], datestring[ 0: 4]

Again, strftime() can be used.

> 19  sys.exit()
>
> In answering the question in line 11 as to when is my next appointment, I
> would answer "3" (for 2/3/2010) but how do I change "days=1" at end of line
> 13 to "days=3" so that datestring would read "2010-02-03" instead of
> "2010-02-01" as presented above?

See comments inline also. You will need to convert your raw_input
result future to a number with "int()" and store it in a new variable
wich can be used in datetime.timedelta().

Greets
Sander

[1] http://docs.python.org/library/time.html#time.strftime
[2] http://docs.python.org/tutorial/errors.html


More information about the Tutor mailing list