Why does this choke?

S Kemplay skemplay at dingley.net
Fri Nov 7 12:31:00 CET 2003


Hi all,

I wrote a script to choose random dates for a statistics assignment.
I only need to choose 30 dates from one year with no leap years and it works 
fine. However I tested with different numbers of dates. It hangs from 450 up. 
I only need 30 dates but it would be good to know why it hangs. (My coding 
probably has something to do with it :))


import random

def getmonth():
    month = random.randint(1,12)
    return month

def getday(month, leaps):
    thirtyones = [1,3,5,7,8,10,12]           
    thirties = [4,6,9,11]                
    if month in thirtyones:
        day = random.randint(1,31)
    elif month in thirties:
        day = random.randint(1,30)
    else:                  
        if leaps == 1: leap = random.randint(1,4)
        else: leap = 1
        if leap in [2,3,4]:
            day = random.randint(1,29)
        else:
            day = random.randint(1,28)
    return day

def getdates(n, leaps):
    dates = []
    i = 0
    while i < n:
        month = getmonth()
        day = getday(month, leaps)
        if (day, month) in dates: 
            continue
        i += 1
        dates.append((day, month))
    return dates


Thanks

Sean Kemplay






More information about the Python-list mailing list