< http://py77.python.pastebin.com/f7191854a> is my addDaysToDate.py . As an exercise I'm trying to handle every kind of exception that can result from user entry.

One I can't figure out is this. The program asks for a date, then a number of days or weeks. A new date is calculated--the date entered plus the number of days. Works fine, except that if the user enters a negative number of days "small" enough for the correct answer to be earlier than 01/01/0001. mxDateTime can't handle such dates. (Nor can it handle dates later than 12/31/9999.) The error produced is

Traceback (most recent call last):
  File "e:\PythonWork\Finished\addDaysToDate.py", line 97, in <module>
    main()
  File "e:\PythonWork\Finished\addDaysToDate.py", line 92, in main
    printDatePlusN(date, n, flag)
  File "e:\PythonWork\Finished\addDaysToDate.py", line 79, in printDatePlusN
    year, month, day = int(date2[0:4]), int(date2[5:7]), int(date2[8:10])
ValueError: invalid literal for int() with base 10: '0/(.'
Process terminated with an exit code of 1

Line 79 is
year, month, day = int(date2[0:4]), int(date2[5:7]), int(date2[8:10])
which is inside printDatePlusN(). Is there something I can put there that would catch the exception and tell the user at that he'd gone back before 01/01/0001 and would have to start over?

(Maybe an example is in order here. Say the user enters today's date, 5/8/2008. If he then enters a diff of -733169, he gets the answer of "01/01/0001". But if he enters -733170 he causes the above error.)

(If the truth be told, I think it should be possible, as soon as he enters the date, to calculate the maximum "negative" diff the user can enter safely. Save this and use it to keep him entering a greater negative diff with getDiff(). However, I couldn't get the (date - "01/01/0001") subtraction to work. They have to be the correct type of objects in order to subtract one from another.)

Thanks,

Dick Moores
 
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to