[Tutor] I need ideas please

Jeff Shannon jeff@ccvcorp.com
Tue May 27 13:49:01 2003


GREENDAY31087@aol.com wrote:

> Anyway, my first problem is the months. Determining what your sign is 
> depends on the month but it's not like every sign starts on the first 
> day of the month. (march 1st-31st: you are a Pisces) the different 
> signs start in the middle of the month. So you cant just take the 
> input (month) and determine what sign corresponds with it. I was 
> thinking about making each month a certain value and then asking the 
> user the day of the month they were born. After that, the program 
> could add the day to the month value to make a new value. I could 
> assign a different sign to each range of numbers and if, say, the 
> value was in the range that 'Pisces' covers, they would be Pisces. 
> Follow me so far?


You should look into the time module.  There's two standard methods of 
representing time (which includes dates): Unix standard, expressed as 
number of seconds since Jan 1st, 1970, and a 9-tuple format with fields 
representing year, month, day, hours, minutes, seconds, etc.  One of the 
fields of that tuple format is the so-called "Julian date", or the day 
of the year (1-366).  You can use these Julian dates to assign an 
unambiguous start date for each astrological sign -- Pisces, for 
example, starts on Feb 19th, which is Julian day 40.

You'll need to get your user's birthday, presumably in month/day form, 
and convert that into a Julian date.  The trick here is in using the 
functions that convert a time tuple into a Unix date (mktime()) and a 
Unix date back into a time tuple (localtime()).  mktime() doesn't 
necessarily require a completely-filled-out 9-member tuple; you can 
specify just year, month, and day, use 0 for the other 6 members, and 
mktime() will react appropriately (this will give you Unix time for 
midnight at the start of that particular day).  Now you can take the 
Unix time that that has given you and convert that back into a time 
tuple, and those previously empty fields will be filled in with the 
proper values.  You can then extract the Julian date, and you're set.

def MakeJulianDate(year, month, day):
    timetuple = (year, month, day) + ( (0,) * 6 ) #pad 9-tuple with 0's
    unixtime = time.mktime(timetuple)
    timetuple = time.localtime(unixtime)
    return timetuple[7]  # number of julian-date field

I've used a bit of trickery to build my time tuple without counting out 
six 0's -- I use multiplication to create a tuple with six 0's, and then 
add that to a tuple of the known date fields to create a new, 9-member 
tuple.  It's up to you whether you want to use the current year or to 
ask the user their birth year (or just use an arbitrary year, like 2000).

> My next problem is not as complicated as the first one but I don't 
> know what should be made into a function to be called. Should I make 
> the whole month thing into a function or should I not define any 
> functions? I'm sort of confused. Also, I wanted to know ... will this 
> program probably need to use a list?


You definitely should define functions.  I've already shown one example 
-- a function that converts a "regular" month/day date into a Julian 
date.  You'll probably also want a function that asks the user for their 
birthday, and returns the year, month, and day.  Finding the correct 
sign based off of the Julian date should probably be a third function.

One way to decide what to put into separate functions, is to describe 
what you're trying to do as a series of steps.  "First, I get the 
birthdate.  Then, I convert that into a format I can use more easily. 
 Next, I figure out what sign that date belongs to.  Finally, I print 
out the sign and maybe some extra information about it."  Each of those 
steps is a likely candidate to become a separate function.  If a given 
step seems to be complicated, you might break that down into substeps, 
each of which is done by another function.

As for needing to use a list... probably, but it depends on how you want 
to do things.  You could set this up as a nested list -- each sign is 
represented by a list of its start date, the name of the sign, and maybe 
a descriptive paragraph.  You then create a list of these sub-lists.   
 By using the start date as the first element of each sub-list, they'll 
easily sort into the proper order.  Note that Capricorn's sub-list may 
need to appear twice -- once starting at Day 1, and a second time 
starting at Day 356 -- since it applies both at the beginning and the 
end of the year.  You can start at the beginning of the list, and for 
each following element, see if the birthdate is greater than the sign's 
start date.

Hope that this gives you some ideas.  If you have further questions, 
don't be afraid to ask further on the list.  

Jeff Shannon
Technician/Programmer
Credit International