[Tutor] Python Assignment Question

Prasad, Ramit ramit.prasad at jpmorgan.com
Mon Sep 24 22:32:38 CEST 2012


Aija Thompson wrote:
> Hi!
> 
> I've been working on this question for a long time and for some reason it's
> just not clicking.
> 
> I'm not sure if my loop for the question is the right one, or if I'm even on
> the right track.
> 
> We're supposed to make a program that counts the number of days into the year
> it is if you input a date. Not including leap years.
> 
> This is what I have so far:
> 
> months = 'January, February, March, April, May, June, July, August, September,
> October, November, December'

This is a string of months. That will not be very helpful to you.
Looking at your later code this should be a list.

months = [ 'January', 'February',.... ]

> daysPerMonth = '31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31'

Same problem here, but given your months I would use a dictionary
so you can map the month to the days per month.

months = { 1 : 31, 2 : 28 }

Note the lack of quotes around 31 and 28. That means these are
Python numbers and not strings. Numbers will be more useful
while calculating. Instead of using the month names,
I figured it would be more useful to know the month number.

> 
> #shorterMonths = 'February, April, June, September, Novmeber'
> #longerMonths = 'January, March, May, July, August, October, December'
> 
> month = raw_input('Enter the month: ')
> day = raw_input('Enter the day: ')
> 
> for x in month:
>         whichMonth = months.index(x)
>         monthNumber = daysPerMonth[whichMonth]
>         dayYear.append(monthNumber)

If you iterate through months (unlike the month as written) it 
would iterate through all months regardless if x is December 
while the date input is say August.

# Untested
days = 0
month = int( raw_input( 'month: ' ) )
day_of_month = int( raw_input( 'day: ' ) )
current_month = 1
while current_month < month:
    # For all months before desired month
    days += months[ current_month ]
# Now add days for current month.
days += day_of_month

> 
> I was hoping to make a loop that would check which month it is and compare it
> to the second list I made with the number of days in that month. I don't know
> if I've made that clear to the program or not. Either way, I feel pretty lost
> at the moment.

Although the above should work, it is not very flexible. 
I would probably look at the date class in the datetime module.
You can use the date class to compare dates and increment the days.
This has the advantage of taking into account things like leap years 
and paves the way for calculating days between ranges (which I suspect
will be an upcoming assignment if you are a student). Of course,
this is computationally inefficient but it should be "fast enough"
and work. The pseudo-code is written below. 

while current_date < desired_date
    days +=1
    current_date += 1 # look at the datetime.timedelta class to do this

> 
> Any help would do!
> 
> Thanks!

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list