Ok if anyone knows how to do python programming please help, this is my last assignment and i cant figure out how i should start this here are the directions...

You are to write a Python program that randomly generates birthdays – considering only the month and day. For example, one birthday you might generate could be: March 2. Another birthday that you might generate could be November 22. Your program must keep track of all of the birthdays that you generate in some sort of list data structure. (You can decide on the details of how you use one or more lists.) Your program should continue generating random birthdays until the first time you generate a birthday twice.

Your program must print out the following output:
1. The complete list of birthdays that you generated.
2. The birthday that was generated twice (printed separately from the list).
3. The count of birthdays the occurred in each month.


You are encouraged to spend some time thinking about how you will write this program. You need to decide which data structure(s) you want. Note you must use lists (somehow) to store the birthdays. You need to decide how you will represent the birthdays internally. Will you use month names? Month numbers? Both? You could even represent birthdays as a number between 1 and 365 but then you would need to convert them to Month/Day format for printing. You need to decide how you will use the random module to generate a random birthday. You need to decide how you will structure your program. How many functions will you use? Finally, you should consider testing and developing small pieces, as you go along.

Please help, how should i start going about starting this, im trying to decide do i make two seperate lists of months and days or combine them all?

Please help thanks

Recommended Answers

All 13 Replies

I like this suggestion of random number n between 0..364 and comparing to set of earlier generated birth day numbers, and then converting to Month/day acccording to list of days until end of month of each month of year (or using built in function to calculate date n days after 1 Jan) and inserting the dates to result list by append.

If you go with the random number as a day in the year, then you can use datetime.datetime.strptime(str(number),"%j") to generate an actual date (in year 1900). The range of day number strings is "1" through "366" (though if you want to deal with leap years, you need to specify a date string with an actual leap year in it, and %Y in the format string.

This is what i have so far please let me know how to compare the new birthday value to the list of birthdays so it can end.

from random import *

listOfMonths = ["January","February","March","April","May","June","July","August","September","October","November","December"]
listOfDays = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
listOfBirthdays = []

def birthdays():
    day = choice(listOfDays)
    month = choice(listOfMonths)
    birthday = month + ' ' + str(day)
    listOfBirthdays.append(birthday)
    return birthday
    
    
def main():
    birthdays()
    
       
    while True:
        newbday = birthdays()
        if birthdays() == listOfBirthdays:
            print "end"
            break
        else:
            listOfBirthdays.append(newbday)
            print newbday

I dislike where you are headed because it is going to be really difficult to deal with what it means if your program selects (as an example) February 30. Tonyjv's suggestion is much easier to deal with. Just for fun, I wrote most of the needed code using his and my suggestions. You should not copy this code blindly as you will probably have to defend it to your teacher. Feel free to use it after you understand it. Or perhaps better, rewrite it using techniques your already understand.

import random
import datetime
yeardays = range(1,366) # ignore leap year
def anotherBirthday():
  yield random.choice(yeardays)

def main():
  seenSoFar = []
  day = anotherBirthday().next()
  while not day in seenSoFar:
    seenSoFar.append(day)
    day = anotherBirthday().next()

  # Get here and you have seen the duplicate
  seenSoFar.append(day) # if you need it in there twice. Spec unclear
  seenSoFarAsDatetime = [datetime.datetime.strptime(str(x),"%j") for x in seenSoFar]
  # specification 1:
  print "This is the list of generated birthdays (length = %d)"%len(seenSoFar)
  print "   "+"\n   ".join([x.strftime("%m-%d") for x in seenSoFarAsDatetime])

  # specification 2:
  print seenSoFarAsDatetime[-1].strftime("The duplicate birthday is %m-%d")

  # specification 3:
  #  I will leave as an exercise.
  #  Check here: http://docs.python.org/library/datetime.html#datetime-objects
  #     for the datetime class attributes
  # I would probably use a map to hold months and counts

main()

so there isnt a way to do how im going about doing this problem? i just feel lost with what you have, im new to python and dont know a whole lot

I leave out the definition of birthday by randint, month first.

from random import randint
listOfMonths = ["January","February","March","April",
                "May","June","July","August","September",
                "October","November","December"]

listOfDays = [31, 28, 31, 30,
              31, 30, 31, 31,
              31, 30, 30, 31]

listOfBirthdays = []

def birthday():
     pass ## your code here
birthd= list()

d = birthday()
while d not in birthd:
    birthd.append(d)
    d = birthday()

birthd.append(d) ## add double
print d ,'two times' ## can not add to set two times
print birthd

That line 10 of course is left over from old code and should be removed or replace birthd with listOfBirthdays.

import random
from datetime import date

def generateBday():
    while True:
        month = random.randint(1, 13)
        day = random.randint(1, 31) # ignore the days with lesser
        try:
            bday = date(2010, month, day) # ignore the year
        except ValueError:
            continue
        return bday
 
def main():
    li = []
    # Convert to whatever format you want with the Date library: http://docs.python.org/library/datetime.html
    li.append((bday.month, bday.day))

if __name__ == "__main__":
    main()

tonyvj thanks for the help but im still unable to get it, should i use if statements? i havent learned a whole lot yet

Umm. If you are asking that level of question, you really aren't ready to do this problem yet.

If you already program, but don't know Python, you might consider looking at the "read me first" thread in this Python group: Lots of good suggestions about how to start programming in Python.

If you have never written programs in any language, then you need to get some face to face help with how to think about writing programs. It will be much more efficient than trying to get that kind of insight from a forum like this one.

index the listOfdays by month 0..11 to find correct maximum for the chosen month and generate number smaller or equal to that. randint produces both number in lower and upper limit. Change month number in range 0..11 to name by indexing listOfMonths.

ya im still confused i just really am not understanding how to do this :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.